欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java工具类DateUtils实例详解

程序员文章站 2023-12-02 19:41:52
本文实例为大家分享了java工具类dateutils的具体代码,供大家参考,具体内容如下 import java.text.parseexception; im...

本文实例为大家分享了java工具类dateutils的具体代码,供大家参考,具体内容如下

import java.text.parseexception;
import java.text.simpledateformat;
import java.util.calendar;
import java.util.date;
/**
 * 描述:公共日期工具类
 */
public class dateutils {

  public static string date_format = "yyyy-mm-dd";

  public static string date_time_format = "yyyy-mm-dd hh:mm:ss";

  public static string date_format_chinese = "yyyy年m月d日";

  /**
   * 获取当前日期
   * 
   * 
   * @return
   * 
   */
  public static string getcurrentdate() {
    string datestr = null;
    simpledateformat df = new simpledateformat(dateutils.date_format);
    datestr = df.format(new date());
    return datestr;
  }

  /**
   * 获取当前日期时间
   * 
   * 
   * @return
   * 
   */
  public static string getcurrentdatetime() {
    string datestr = null;
    simpledateformat df = new simpledateformat(dateutils.date_time_format);
    datestr = df.format(new date());
    return datestr;
  }

  /**
   * 获取当前日期时间
   * 
   * 
   * @return
   * 
   */
  public static string getcurrentdatetime(string dateformat) {
    string datestr = null;
    simpledateformat df = new simpledateformat(dateformat);
    datestr = df.format(new date());
    return datestr;
  }

  public static string datetodatetime(date date) {
    string datestr = null;
    simpledateformat df = new simpledateformat(dateutils.date_time_format);
    datestr = df.format(date);
    return datestr;
  }
  /**
   * 将字符串日期转换为日期格式
   * 
   * 
   * @param datestr
   * @return
   * 
   */
  public static date stringtodate(string datestr) {

      if(datestr ==null ||datestr.equals("")){
        return null;
      }
    date date = new date();
    simpledateformat df = new simpledateformat(dateutils.date_format);
    try {
      date = df.parse(datestr);
    } catch (parseexception e) {
      date=dateutils.stringtodate(datestr,"yyyymmdd");
    }
    return date;
  }

  /**
   * 将字符串日期转换为日期格式
   * 自定義格式
   * 
   * @param datestr
   * @return
   * 
   */
  public static date stringtodate(string datestr, string dateformat) {
    date date = new date();
    simpledateformat df = new simpledateformat(dateformat);
    try {
      date = df.parse(datestr);
    } catch (parseexception e) {
      e.printstacktrace();
    }
    return date;
  }




  /**
   * 将日期格式日期转换为字符串格式
   * 
   * 
   * @param date
   * @return
   * 
   */
  public static string datetostring(date date) {
    string datestr = null;
    simpledateformat df = new simpledateformat(dateutils.date_format);
    datestr = df.format(date);
    return datestr;
  }

  /**
   * 将日期格式日期转换为字符串格式 自定義格式
   * 
   * @param date
   * @param dateformat
   * @return
   */
  public static string datetostring(date date, string dateformat) {
    string datestr = null;
    simpledateformat df = new simpledateformat(dateformat);
    datestr = df.format(date);
    return datestr;
  }

  /**
   * 获取日期的day值
   * 
   * 
   * @param date
   *      输入日期
   * @return
   * 
   */
  public static int getdayofdate(date date) {
    int d = 0;
    calendar cd = calendar.getinstance();
    cd.settime(date);
    d = cd.get(calendar.day_of_month);
    return d;
  }

  /**
   * 获取日期的month值
   * 
   * 
   * @param date
   *      输入日期
   * @return
   * 
   */
  public static int getmonthofdate(date date) {
    int m = 0;
    calendar cd = calendar.getinstance();
    cd.settime(date);
    m = cd.get(calendar.month) + 1;
    return m;
  }

  /**
   * 获取日期的year值
   * 
   * 
   * @param date
   *      输入日期
   * @return
   * 
   */
  public static int getyearofdate(date date) {
    int y = 0;
    calendar cd = calendar.getinstance();
    cd.settime(date);
    y = cd.get(calendar.year);
    return y;
  }

  /**
   * 获取星期几
   * 
   * 
   * @param date
   *      输入日期
   * @return
   * 
   */
  public static int getweekofdate(date date) {
    int wd = 0;
    calendar cd = calendar.getinstance();
    cd.settime(date);
    wd = cd.get(calendar.day_of_week) - 1;
    return wd;
  }

  /**
   * 获取输入日期的当月第一天
   * 
   * 
   * @param date
   *      输入日期
   * @return
   * 
   */
  public static date getfirstdayofmonth(date date) {
    calendar cd = calendar.getinstance();
    cd.settime(date);
    cd.set(calendar.day_of_month, 1);
    return cd.gettime();
  }

  /**
   * 获得输入日期的当月最后一天
   * 
   * @param date
   */
  public static date getlastdayofmonth(date date) {
    return dateutils.addday(dateutils.getfirstdayofmonth(dateutils.addmonth(date, 1)), -1);
  }

  /**
   * 判断是否是闰年
   * 
   * 
   * @param date
   *      输入日期
   * @return 是true 否false
   * 
   */
  public static boolean isleapyear(date date) {

    calendar cd = calendar.getinstance();
    cd.settime(date);
    int year = cd.get(calendar.year);

    if (year % 4 == 0 && year % 100 != 0 | year % 400 == 0) {
      return true;
    } else {
      return false;
    }
  }

  /**
   * 根据整型数表示的年月日,生成日期类型格式
   * 
   * @param year
   *      年
   * @param month
   *      月
   * @param day
   *      日
   * @return
   * 
   */
  public static date getdatebyymd(int year, int month, int day) {
    calendar cd = calendar.getinstance();
    cd.set(year, month-1, day);
    return cd.gettime();
  }

  /**
   * 获取年周期对应日
   * 
   * @param date
   *      输入日期
   * @param iyear
   *      年数  負數表示之前
   * @return
   * 
   */
  public static date getyearcycleofdate(date date, int iyear) {
    calendar cd = calendar.getinstance();
    cd.settime(date);

    cd.add(calendar.year, iyear);

    return cd.gettime();
  }

  /**
   * 获取月周期对应日
   * 
   * @param date
   *      输入日期
   * @param i
   * @return
   * 
   */
  public static date getmonthcycleofdate(date date, int i) {
    calendar cd = calendar.getinstance();
    cd.settime(date);

    cd.add(calendar.month, i);

    return cd.gettime();
  }

  /**
   * 计算 fromdate 到 todate 相差多少年
   * 
   * @param fromdate
   * @param todate
   * @return 年数
   * 
   */
  public static int getyearbyminusdate(date fromdate, date todate) {
    calendar df=calendar.getinstance();
    df.settime(fromdate);

    calendar dt=calendar.getinstance();
    dt.settime(todate);

    return dt.get(calendar.year)-df.get(calendar.year);
  }

  /**
   * 计算 fromdate 到 todate 相差多少个月
   * 
   * @param fromdate
   * @param todate
   * @return 月数
   * 
   */
  public static int getmonthbyminusdate(date fromdate, date todate) {
    calendar df=calendar.getinstance();
    df.settime(fromdate);

    calendar dt=calendar.getinstance();
    dt.settime(todate);

    return dt.get(calendar.year)*12+dt.get(calendar.month)-
        (df.get(calendar.year)*12+df.get(calendar.month));
  }

  /**
   * 计算 fromdate 到 todate 相差多少天
   * 
   * @param fromdate
   * @param todate
   * @return 天数
   * 
   */
  public static long getdaybyminusdate(object fromdate, object todate) {

    date f=dateutils.chgobject(fromdate);

    date t=dateutils.chgobject(todate);

    long fd=f.gettime();
    long td=t.gettime();

    return (td-fd)/(24l * 60l * 60l * 1000l);
  }

  /**
   * 计算年龄
   * 
   * @param birthday
   *      生日日期
   * @param calcdate
   *      要计算的日期点
   * @return
   * 
   */
  public static int calcage(date birthday, date calcdate) {

    int cyear=dateutils.getyearofdate(calcdate);
    int cmonth=dateutils.getmonthofdate(calcdate);
    int cday=dateutils.getdayofdate(calcdate);   
    int byear=dateutils.getyearofdate(birthday);
    int bmonth=dateutils.getmonthofdate(birthday);
    int bday=dateutils.getdayofdate(birthday);

    if(cmonth>bmonth||(cmonth==bmonth&&cday>bday)){
      return cyear-byear;
    }else{
      return cyear-1-byear;
    }
  }

  /**
   * 从身份证中获取出生日期
   * 
   * @param idno
   *      身份证号码
   * @return
   * 
   */
  public static string getbirthdayfromidcard(string idno) {
    calendar cd = calendar.getinstance();
    if (idno.length() == 15) {
      cd.set(calendar.year, integer.valueof("19" + idno.substring(6, 8))
          .intvalue());
      cd.set(calendar.month, integer.valueof(idno.substring(8, 10))
          .intvalue() - 1);
      cd.set(calendar.day_of_month,
          integer.valueof(idno.substring(10, 12)).intvalue());
    } else if (idno.length() == 18) {
      cd.set(calendar.year, integer.valueof(idno.substring(6, 10))
          .intvalue());
      cd.set(calendar.month, integer.valueof(idno.substring(10, 12))
          .intvalue() - 1);
      cd.set(calendar.day_of_month,
          integer.valueof(idno.substring(12, 14)).intvalue());
    }
    return dateutils.datetostring(cd.gettime());
  }

  /**
   * 在输入日期上增加(+)或减去(-)天数
   * 
   * @param date
   *      输入日期
   * @param imonth
   *      要增加或减少的天数
   */
  public static date addday(date date, int iday) {
    calendar cd = calendar.getinstance();

    cd.settime(date);

    cd.add(calendar.day_of_month, iday);

    return cd.gettime();
  }

  /**
   * 在输入日期上增加(+)或减去(-)月份
   * 
   * @param date
   *      输入日期
   * @param imonth
   *      要增加或减少的月分数
   */
  public static date addmonth(date date, int imonth) {
    calendar cd = calendar.getinstance();

    cd.settime(date);

    cd.add(calendar.month, imonth);

    return cd.gettime();
  }

  /**
   * 在输入日期上增加(+)或减去(-)年份
   * 
   * @param date
   *      输入日期
   * @param imonth
   *      要增加或减少的年数
   */
  public static date addyear(date date, int iyear) {
    calendar cd = calendar.getinstance();

    cd.settime(date);

    cd.add(calendar.year, iyear);

    return cd.gettime();
  }

  /**
   * 將object類型轉換為date
   * @param date
   * @return
   */
  public static date chgobject(object date){

    if(date!=null&&date instanceof date){
      return (date)date;
    }

    if(date!=null&&date instanceof string){
      return dateutils.stringtodate((string)date);
    }

    return null;

  }

  public static long getagebybirthday(string date){

    date birthday = stringtodate(date, "yyyy-mm-dd");
    long sec = new date().gettime() - birthday.gettime();

    long age = sec/(1000*60*60*24)/365;

    return age;
  }


  /**
   * @param args
   */
  public static void main(string[] args) {
    //string temp = dateutil.datetostring(getlastdayofmonth(new date()),
    ///   dateutil.date_format_chinese);
    //string s=dateutil.datetostring(dateutil.addday(dateutil.addyear(new date(),1),-1));


    long s=dateutils.getdaybyminusdate("2012-01-01", "2012-12-31");
    system.err.println(s);


  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。