国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Java工具類DateUtils實例詳解

2024-07-13 10:14:21
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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);  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临高县| 手机| 邹平县| 正蓝旗| 凤山市| 呼和浩特市| 大荔县| 米林县| 古交市| 金阳县| 昔阳县| 胶南市| 上思县| 巴里| 镇赉县| 乐陵市| 陇西县| 天柱县| 宜阳县| 徐汇区| 奎屯市| 孟津县| 连城县| 仙居县| 韩城市| 甘南县| 赤壁市| 红河县| 石城县| 大新县| 竹溪县| 孝昌县| 噶尔县| 广德县| 上饶县| 海原县| 永嘉县| 渝北区| 平遥县| 德安县| 通城县|