日期工具类DataTool

本文介绍了一个实用的Java日期处理工具类,提供了多种日期格式化、计算日期差、获取当前日期等常用功能,并附带详细的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java代码 复制代码
  1. /**
  2. * DateTool.java
  3. * 版权所有(C) 2011 cuiran2001@163.com
  4. * 创建:CuiRan  2011-6-10 上午10:47:15
  5. */ 
  6.  
  7. package com.yinzhijie.sms.appclient.base; 
  8.  
  9. import java.math.BigDecimal; 
  10. import java.text.ParseException; 
  11. import java.text.SimpleDateFormat; 
  12. import java.util.Calendar; 
  13. import java.util.Date; 
  14.  
  15. /**
  16. * @author CuiRan
  17. * @version 1.0.0
  18. * @desc
  19. */ 
  20. public class DateTool { 
  21.     /**
  22.      * 缺省的日期显示格式: yyyy-MM-dd
  23.      */ 
  24.     public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd"
  25.  
  26.     /**
  27.      * 缺省的日期时间显示格式:yyyy-MM-dd HH:mm:ss
  28.      */ 
  29.     public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss"
  30.      
  31.     /**
  32.      * 1s中的毫秒数
  33.      */ 
  34.     private static final int MILLIS = 1000
  35.      
  36.     /**
  37.      * 一年当中的月份数
  38.      */ 
  39.     private static final int MONTH_PER_YEAR = 12
  40.  
  41.     /**
  42.      * 私有构造方法,禁止对该类进行实例化
  43.      */ 
  44.     private DateTool() { 
  45.     } 
  46.  
  47.     /**
  48.      * 得到系统当前日期时间
  49.      *
  50.      * @return 当前日期时间
  51.      */ 
  52.     public static Date getNow() { 
  53.         return Calendar.getInstance().getTime(); 
  54.     } 
  55.  
  56.     /**
  57.      * 得到用缺省方式格式化的当前日期
  58.      *
  59.      * @return 当前日期
  60.      */ 
  61.     public static String getDate() { 
  62.         return getDateTime(DEFAULT_DATE_FORMAT); 
  63.     } 
  64.  
  65.     /**
  66.      * 得到用缺省方式格式化的当前日期及时间
  67.      *
  68.      * @return 当前日期及时间
  69.      */ 
  70.     public static String getDateTime() { 
  71.         return getDateTime(DEFAULT_DATETIME_FORMAT); 
  72.     } 
  73.  
  74.     /**
  75.      * 得到系统当前日期及时间,并用指定的方式格式化
  76.      *
  77.      * @param pattern 显示格式
  78.      * @return 当前日期及时间
  79.      */ 
  80.     public static String getDateTime(String pattern) { 
  81.         Date datetime = Calendar.getInstance().getTime(); 
  82.         return getDateTime(datetime, pattern); 
  83.     } 
  84.      
  85.  
  86.     /**
  87.      * 得到用指定方式格式化的日期
  88.      *
  89.      * @param date 需要进行格式化的日期
  90.      * @param pattern 显示格式
  91.      * @return 日期时间字符串
  92.      */ 
  93.     public static String getDateTime(Date date, String pattern) { 
  94.         if (null == pattern || "".equals(pattern)) { 
  95.             pattern = DEFAULT_DATETIME_FORMAT; 
  96.         } 
  97.         SimpleDateFormat dateFormat = new SimpleDateFormat(pattern); 
  98.         return dateFormat.format(date); 
  99.     } 
  100.  
  101.     /**
  102.      * 得到当前年份
  103.      *
  104.      * @return 当前年份
  105.      */ 
  106.     public static int getCurrentYear() { 
  107.         return Calendar.getInstance().get(Calendar.YEAR); 
  108.          
  109.     } 
  110.  
  111.     /**
  112.      * 得到当前月份
  113.      *
  114.      * @return 当前月份
  115.      */ 
  116.     public static int getCurrentMonth() { 
  117.         //用get得到的月份数比实际的小1,需要加上 
  118.         return Calendar.getInstance().get(Calendar.MONTH) + 1
  119.     } 
  120.  
  121.     /**
  122.      * 得到当前日
  123.      *
  124.      * @return 当前日
  125.      */ 
  126.     public static int getCurrentDay() { 
  127.         return Calendar.getInstance().get(Calendar.DATE); 
  128.     } 
  129.  
  130.     /**
  131.      * 取得当前日期以后若干天的日期。如果要得到以前的日期,参数用负数。 例如要得到上星期同一天的日期,参数则为-7
  132.      *
  133.      * @param days 增加的日期数
  134.      * @return 增加以后的日期
  135.      */ 
  136.     public static Date addDays(int days) { 
  137.         return add(getNow(), days, Calendar.DATE); 
  138.     } 
  139.  
  140.     /**
  141.      * 取得指定日期以后若干天的日期。如果要得到以前的日期,参数用负数。
  142.      *
  143.      * @param date 基准日期
  144.      * @param days 增加的日期数
  145.      * @return 增加以后的日期
  146.      */ 
  147.     public static Date addDays(Date date, int days) { 
  148.         return add(date, days, Calendar.DATE); 
  149.     } 
  150.  
  151.     /**
  152.      * 取得当前日期以后某月的日期。如果要得到以前月份的日期,参数用负数。
  153.      *
  154.      * @param months 增加的月份数
  155.      * @return 增加以后的日期
  156.      */ 
  157.     public static Date addMonths(int months) { 
  158.         return add(getNow(), months, Calendar.MONTH); 
  159.     } 
  160.  
  161.     /**
  162.      * 取得指定日期以后某月的日期。如果要得到以前月份的日期,参数用负数。
  163.      *  注意,可能不是同一日子,例如2003-1-31加上一个月是2003-2-28
  164.      *
  165.      * @param date 基准日期
  166.      * @param months 增加的月份数
  167.      * @return 增加以后的日期
  168.      */ 
  169.     public static Date addMonths(Date date, int months) { 
  170.         return add(date, months, Calendar.MONTH); 
  171.     } 
  172.  
  173.     /**
  174.      * 内部方法。为指定日期增加相应的天数或月数
  175.      *
  176.      * @param date 基准日期
  177.      * @param amount 增加的数量
  178.      * @param field 增加的单位,年,月或者日
  179.      * @return 增加以后的日期
  180.      */ 
  181.     private static Date add(Date date, int amount, int field) { 
  182.         Calendar calendar = Calendar.getInstance(); 
  183.  
  184.         calendar.setTime(date); 
  185.         calendar.add(field, amount); 
  186.  
  187.         return calendar.getTime(); 
  188.     } 
  189.     /**
  190.      * 通过date对象取得格式为小时:分钟的实符串
  191.      * @return
  192.      */ 
  193.     @SuppressWarnings("deprecation"
  194.     public static String getHourMin(Date date){      
  195.          StringBuffer sf = new StringBuffer();         
  196.          sf.append(date.getHours()); 
  197.          sf.append(":"); 
  198.          sf.append(date.getMinutes()); 
  199.          return sf.toString(); 
  200.     } 
  201.  
  202.     /**
  203.      * 计算两个日期相差天数。 用第一个日期减去第二个。如果前一个日期小于后一个日期,则返回负数
  204.      *
  205.      * @param one 第一个日期数,作为基准
  206.      * @param two 第二个日期数,作为比较
  207.      * @return 两个日期相差天数
  208.      */ 
  209.     public static long diffDays(Date one, Date two) { 
  210.         Calendar calendar = Calendar.getInstance(); 
  211.         calendar.clear(); 
  212.         calendar.setTime(one); 
  213.         calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY) 
  214.                 , calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0); 
  215.         Date d1 = calendar.getTime(); 
  216.         calendar.clear(); 
  217.         calendar.setTime(two); 
  218.         calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY) 
  219.                 , calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0); 
  220.         Date d2 = calendar.getTime(); 
  221.         final int MILISECONDS = 24 * 60 * 60 * 1000
  222.         BigDecimal r = new BigDecimal(new Double((d1.getTime() - d2.getTime()))  
  223.         / MILISECONDS);         
  224.         return Math.round(r.doubleValue()); 
  225.     } 
  226.  
  227.     /**
  228.      * 计算两个日期相差月份数 如果前一个日期小于后一个日期,则返回负数
  229.      *
  230.      * @param one 第一个日期数,作为基准
  231.      * @param two 第二个日期数,作为比较
  232.      * @return 两个日期相差月份数
  233.      */ 
  234.     public static int diffMonths(Date one, Date two) { 
  235.  
  236.         Calendar calendar = Calendar.getInstance(); 
  237.  
  238.         //得到第一个日期的年分和月份数 
  239.         calendar.setTime(one); 
  240.         int yearOne = calendar.get(Calendar.YEAR); 
  241.         int monthOne = calendar.get(Calendar.MONDAY); 
  242.         //得到第二个日期的年份和月份 
  243.         calendar.setTime(two); 
  244.         int yearTwo = calendar.get(Calendar.YEAR); 
  245.         int monthTwo = calendar.get(Calendar.MONDAY); 
  246.  
  247.         return (yearOne - yearTwo) * MONTH_PER_YEAR + (monthOne - monthTwo); 
  248.     } 
  249.      
  250.     /**
  251.      * 获取某一个日期的年份
  252.      * @param d
  253.      * @return
  254.      */ 
  255.     public static int getYear(Date d) { 
  256.         Calendar calendar = Calendar.getInstance(); 
  257.         calendar.setTime(d); 
  258.         return calendar.get(Calendar.YEAR); 
  259.     } 
  260.  
  261.     /**
  262.      * 将一个字符串用给定的格式转换为日期类型。 <br>
  263.      * 注意:如果返回null,则表示解析失败
  264.      *
  265.      * @param datestr 需要解析的日期字符串
  266.      * @param pattern 日期字符串的格式,默认为"yyyy-MM-dd"的形式
  267.      * @return 解析后的日期
  268.      */ 
  269.     public static Date parse(String datestr, String pattern) { 
  270.         Date date = null
  271.          
  272.         if (null == pattern || "".equals(pattern)) { 
  273.             pattern = DEFAULT_DATE_FORMAT; 
  274.         } 
  275.  
  276.         try
  277.             SimpleDateFormat dateFormat = new SimpleDateFormat(pattern); 
  278.             date = dateFormat.parse(datestr); 
  279.         } catch (ParseException e) { 
  280.             e.printStackTrace(); 
  281.         } 
  282.  
  283.         return date; 
  284.     } 
  285.  
  286.     /**
  287.      * 返回本月的最后一天
  288.      *
  289.      * @return 本月最后一天的日期
  290.      */ 
  291.     public static Date getMonthLastDay() { 
  292.         return getMonthLastDay(getNow()); 
  293.     } 
  294.  
  295.     /**
  296.      * 返回给定日期中的月份中的最后一天
  297.      *
  298.      * @param date 基准日期
  299.      * @return 该月最后一天的日期
  300.      */ 
  301.     public static Date getMonthLastDay(Date date) { 
  302.  
  303.         Calendar calendar = Calendar.getInstance(); 
  304.         calendar.setTime(date); 
  305.  
  306.         //将日期设置为下一月第一天 
  307.         calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, 1); 
  308.  
  309.         //减去1天,得到的即本月的最后一天 
  310.         calendar.add(Calendar.DATE, -1); 
  311.  
  312.         return calendar.getTime(); 
  313.     } 
  314.      
  315.     /**
  316.      * 计算两个具体日期之间的秒差,第一个日期-第二个日期
  317.      * @param date1
  318.      * @param date2
  319.      * @param onlyTime  是否只计算2个日期的时间差异,忽略日期,true代表只计算时间差
  320.      * @return
  321.      */ 
  322.     public static long diffSeconds(Date date1,Date date2,boolean onlyTime) { 
  323.         if (onlyTime) { 
  324.             Calendar calendar = Calendar.getInstance(); 
  325.             calendar.setTime(date1); 
  326.             //calendar.set(1984, 5, 24); 
  327.             long t1 = calendar.getTimeInMillis(); 
  328.             calendar.setTime(date2); 
  329.             //calendar.set(1984, 5, 24); 
  330.             long t2 = calendar.getTimeInMillis(); 
  331.             return (t1-t2)/MILLIS; 
  332.         } else
  333.             return (date1.getTime()-date2.getTime())/MILLIS; 
  334.         } 
  335.     } 
  336.      
  337.     /**
  338.      * @param date1
  339.      * @param date2
  340.      * @return
  341.      */ 
  342.     public static long diffSeconds(Date date1,Date date2) { 
  343.         return diffSeconds(date1,date2,false); 
  344.     } 
  345.      
  346.     /**
  347.      * 根据日期确定星期几:1-星期日,2-星期一.....s
  348.      * @param date
  349.      * @return
  350.      */ 
  351.     public static int getDayOfWeek(Date date){ 
  352.            Calendar   cd   =   Calendar.getInstance();    
  353.            cd.setTime(date);    
  354.            int   mydate   = cd.get(Calendar.DAY_OF_WEEK);  
  355.            return mydate; 
  356.     } 
  357.     
  358. //    /** 
  359. //     * 将2010-06-01转换为20100601格式 
  360. //     * @param date 
  361. //     * @return 
  362. //     */ 
  363. //    public static String toVODate(String date) { 
  364. //      if (StringUtil.isEmpty(date)) { 
  365. //          //return DateFormatUtils.format(new Date(), "yyyy-MM-dd"); 
  366. //          return ""; 
  367. //      } 
  368. //      Date tdate; 
  369. //      try { 
  370. //          tdate = new SimpleDateFormat("yyyyMMdd").parse(date); 
  371. //      } catch (ParseException e) { 
  372. //          e.printStackTrace(); 
  373. //          throw new SmsException("日期转换异常!"); 
  374. //      } 
  375. //      return DateFormatUtils.format(tdate, "yyyy-MM-dd"); 
  376. //    } 
  377. //     
  378. //    /** 
  379. //     * 将20100601转换为2010-06-01格式 
  380. //     * @param date 
  381. //     * @return 
  382. //     */ 
  383. //    public static String toDomainDate(String date) { 
  384. //      if (StringUtil.isEmpty(date)) { 
  385. //          return ""; 
  386. //      } 
  387. //      Date tdate; 
  388. //      try { 
  389. //          tdate = new SimpleDateFormat("yyyy-MM-dd").parse(date); 
  390. //      } catch (ParseException e) { 
  391. //          throw new BusinessException("上收时间或者启用时间格式不正确!"); 
  392. //      } 
  393. //      return DateFormatUtils.format(tdate, "yyyyMMdd"); 
  394. //    } 
  395.      
  396.     /**
  397.      * 验证用密码是否在有效期内(跟当前日期比较)
  398.      * @param format "yyyyMMdd"
  399.      * @param validDate
  400.      * @return
  401.      */ 
  402.     public static boolean isValidDate(String validDate, String format) { 
  403.         Date valid = parse(validDate,format); 
  404.         Date now = new Date(); 
  405.         String nowStr = new SimpleDateFormat(format).format(now); 
  406.         try
  407.             now = new SimpleDateFormat(format).parse(nowStr); 
  408.         } catch (ParseException e) { 
  409.             e.printStackTrace(); 
  410.         } 
  411.         return valid.after(now); 
  412.     } 
  413.   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值