java 日期差_java计算时间差, 日期差小结

1、java 7中的日历类Calendar

Calendar类使用其静态的getInstance()方法获取一个日历实例,该实例为当前的时间;如果想改变时间,可以通过其setTime方法传入一个Date对象,即可获得Date对象所表示时间的Calendar对象

1 /**

2 *使用Calendar对象计算时间差,可以按照需求定制自己的计算逻辑3 *@paramstrDate4 *@throwsParseException5 */

6 public static void calculateTimeDifferenceByCalendar(String strDate) throwsParseException {7 SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");8 Date date =formatter.parse(strDate);9

10 Calendar c1 = Calendar.getInstance(); //当前日期

11 Calendar c2 =Calendar.getInstance();12 c2.setTime(date); //设置为另一个时间

13

14 int year =c1.get(Calendar.YEAR);15 int oldYear =c2.get(Calendar.YEAR);16

17 //这里只是简单的对两个年份数字进行相减,而没有考虑月份的情况

18 System.out.println("传入的日期与今年的年份差为:" + (year -oldYear));19 }

输出为:

1 传入的日期与今年的年份差为:12

2、java 8中的周期类Period

通过调用Period类的静态方法between,传入两个待比较的LocalDate对象today与oldDate,得到的Period的对象p中就包含了today与oldDate两个日期相差的年、月、日信息,可以通过p.getYears()等方法取出

1 /**

2 * 使用java 8的Period的对象计算两个LocalDate对象的时间差,严格按照年、月、日计算,如:2018-03-12 与 2014-05-23 相差 3 年 9 个月 17 天3 *@paramyear4 *@parammonth5 *@paramdayOfMonth6 */

7 public static void calculateTimeDifferenceByPeriod(int year, Month month, intdayOfMonth) {8 LocalDate today =LocalDate.now();9 System.out.println("Today:" +today);10 LocalDate oldDate =LocalDate.of(year, month, dayOfMonth);11 System.out.println("OldDate:" +oldDate);12

13 Period p =Period.between(oldDate, today);14 System.out.printf("目标日期距离今天的时间差:%d 年 %d 个月 %d 天\n", p.getYears(), p.getMonths(), p.getDays());15 }

输出为:

1 Today:2018-03-13

2 OldDate:2014-05-23

3 目标日期距离今天的时间差:3 年 9 个月 18 天

3、java 8中的Duration类

Duration与Period相对应,Period用于处理日期,而Duration计算时间差还可以处理具体的时间,也是通过调用其静态的between方法,该方法的签名是between(Temporal startInclusive, Temporal endExclusive),因此可以传入两个Instant的实例(Instant实现了Temporal接口),并可以以毫秒(toMillis)、秒(getSeconds)等多种形式表示得到的时间差

1 public static voidcalculateTimeDifferenceByDuration() {2 Instant inst1 = Instant.now(); //当前的时间

3 System.out.println("Inst1:" +inst1);4 Instant inst2 = inst1.plus(Duration.ofSeconds(10)); //当前时间+10秒后的时间

5 System.out.println("Inst2:" +inst2);6 Instant inst3 = inst1.plus(Duration.ofDays(125)); //当前时间+125天后的时间

7 System.out.println("inst3:" +inst3);8

9 System.out.println("以毫秒计的时间差:" +Duration.between(inst1, inst2).toMillis());10

11 System.out.println("以秒计的时间差:" +Duration.between(inst1, inst3).getSeconds());12 }

输出为:

1 Inst1:2018-03-13T09:06:00.691Z2 Inst2:2018-03-13T09:06:10.691Z3 inst3:2018-07-16T09:06:00.691Z4 以毫秒计的时间差:10000

5 以秒计的时间差:10800000

4、java 8中的ChronoUnit类

ChronoUnit的between方法签名为,between(Temporal temporal1Inclusive, Temporal temporal2Exclusive),需要注意的是,如果要以不同的单位展示时间差,between入参中的时间对象必须包含有对应的时间信息,否则会抛出java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit XXX的异常

1 /**

2 * 使用java 8的ChronoUnit,ChronoUnit可以以多种单位(基本涵盖了所有的,看源码发现竟然还有“FOREVER”这种单位。。)表示两个时间的时间差3 */

4 public static voidcalculateTimeDifferenceByChronoUnit() {5 LocalDate startDate = LocalDate.of(2003, Month.MAY, 9);6 System.out.println("开始时间:" +startDate);7

8 LocalDate endDate = LocalDate.of(2015, Month.JANUARY, 26);9 System.out.println("结束时间:" +endDate);10

11 long daysDiff =ChronoUnit.DAYS.between(startDate, endDate);12 System.out.println("两个时间之间的天数差为:" +daysDiff);13 //long hoursDiff = ChronoUnit.HOURS.between(startDate, endDate);//这句会抛出异常,因为LocalDate表示的时间中不包含时分秒等信息

14 }

输出为:

开始时间:2003-05-09结束时间:2015-01-26两个时间之间的天数差为:4280

5、传统的SimpleDateFormat类

用SimpleDateFormat计算时间差的方法,网上找了一份,自己跑了一遍,可以使用,贴在下面

1 /**

2 * 用SimpleDateFormat计算时间差3 *@throwsParseException4 */

5 public static void calculateTimeDifferenceBySimpleDateFormat() throwsParseException {6 SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");7 /*天数差*/

8 Date fromDate1 = simpleFormat.parse("2018-03-01 12:00");9 Date toDate1 = simpleFormat.parse("2018-03-12 12:00");10 long from1 =fromDate1.getTime();11 long to1 =toDate1.getTime();12 int days = (int) ((to1 - from1) / (1000 * 60 * 60 * 24));13 System.out.println("两个时间之间的天数差为:" +days);14

15 /*小时差*/

16 Date fromDate2 = simpleFormat.parse("2018-03-01 12:00");17 Date toDate2 = simpleFormat.parse("2018-03-12 12:00");18 long from2 =fromDate2.getTime();19 long to2 =toDate2.getTime();20 int hours = (int) ((to2 - from2) / (1000 * 60 * 60));21 System.out.println("两个时间之间的小时差为:" +hours);22

23 /*分钟差*/

24 Date fromDate3 = simpleFormat.parse("2018-03-01 12:00");25 Date toDate3 = simpleFormat.parse("2018-03-12 12:00");26 long from3 =fromDate3.getTime();27 long to3 =toDate3.getTime();28 int minutes = (int) ((to3 - from3) / (1000 * 60));29 System.out.println("两个时间之间的分钟差为:" +minutes);30 }

输出为:

1 两个时间之间的天数差为:11

2 两个时间之间的小时差为:264

3 两个时间之间的分钟差为:15840

总结

传统的SimpleDateFormat和Java 7中的Calendar在使用的时候需要自己写一个计算时间差的逻辑,比较麻烦,但是却比较灵活,方便根据自己具体的需求来定制(比如,我想两个日期的天数差15天就算满一个月,不满15天不算一个月,如2018-01-04到2018-02-20,算2个月的时间差);而Java 8中的几个计算时间差的类更加方便、精确,可以以不同的单位表示得到的时间差,但要注意几个类所包含的时间信息的区别:

1 System.out.println(LocalDate.now()); //只包含日期信息

2 System.out.println(LocalTime.now()); //只包含时间信息

3 System.out.println(LocalDateTime.now()); //包含日期、时间信息

输出为:

1 2018-03-13

2 17:13:26.134

3 2018-03-13T17:13:26.135

以上总结的几个方法只是个例子,具体使用的时候可能需要传入一个或者两个时间进行比较,可能会涉及到这些时间对象的相互转换,Instant、Date、LocalDate等等。。。我就不列举了。。。

另外在使用SimpleDateFormat对String类型的日期进行parse的时候,如果传入的日期为:2017-08-60,这种错误的日期,Java默认会按照日期的信息对其进行转换,formatter.parse("2017-08-60");,得到的日期为2017-09-29,而如果不想进行这种转换,而直接将其判定为输入错误,则可以设置formatter.setLenient(false);,这时就会抛出java.text.ParseException异常了

1 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");2 formatter.setLenient(false);3 try{4 Date date = formatter.parse("2017-08-60"); //抛出转换异常

5 System.out.println(date);6 } catch(ParseException e) {7 e.printStackTrace();8 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值