Java8中计算时间的四种方式及区别Period、Duration、ChronoUnit、Until

本文介绍了Java 8中Period类计算年月日,Duration类计算精确到时分秒,以及ChronoUnit和Until类进行更复杂时间间隔测量。通过实例展示了如何获取LocalDate和LocalDateTime之间的差异,并提供了相关代码和运行结果。

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

一.简述

在Java8中,我们可以使用以下类来计算日期时间差异:

1.Period
2.Duration
3.ChronoUnit

 二.Period类

Period类计算只有年、月、日

计算的是LocalDate两个时间间隔的年月日

public static void main(String[] args) {
        LocalDate startTime = LocalDate.now();
        System.err.println("startTime : " + startTime);
        LocalDate endTime = LocalDate.now().plusMonths(18);
        System.err.println("endTime : " + endTime);

        Period p = Period.between(startTime, endTime);
        System.err.printf("时间间隔 : %d 年 %d 月 %d 日", p.getYears(), p.getMonths(), p.getDays());
    }
运行结果:
startTime : 2022-05-12
endTime : 2023-11-12
时间间隔 : 1 年 6 月 0 日

三.Duration

Duration类计算只有日、时、分、秒、毫秒,

计算的是LocalDateTimel两个时间分别间隔的日、时、分、秒、毫秒

 public static void main(String[] args) {
        LocalDateTime startTime = LocalDateTime.now();
        System.err.println("startTime : " + startTime);
        LocalDateTime endTime = LocalDateTime.now().plusDays(1).plusHours(1).plusMinutes(1).plusMinutes(1);
        System.err.println("endTime : " + endTime);

        Duration between = Duration.between(startTime, endTime);
        System.err.println("日 "+between.toDays());
        System.err.println("时 "+between.toHours());
        System.err.println("分 "+between.toMinutes());
        System.err.println("秒 "+between.getSeconds());
        System.err.println("毫秒"+between.toMillis());
        System.err.printf("时间间隔 : %d 日 %d 时 %d 分 %d 秒 %d 毫秒 ", between.toDays(), between.toHours(),between.toMinutes(),between.getSeconds(),between.toMillis());
    }
运行结果:
startTime : 2022-05-12T17:37:06.426
endTime : 2022-05-13T18:39:06.426
日 1
时 25
分 1502
秒 90120
毫秒90120000
时间间隔 : 1 日 25 时 1502 分 90120 秒 90120000 毫秒 

四.ChronoUnit类

ChronoUnit类计算有年、月、周、日、时、分、秒、毫秒

计算的是LocalDate和LocalDateTime两个时间分别间隔的年、月、周、日、时、分、秒、毫秒

public static void main(String[] args) {
        LocalDateTime startTime = LocalDateTime.now();
        System.err.println("startTime : " + startTime);
        LocalDateTime endTime = LocalDateTime.now().plusYears(1).plusMonths(1).plusWeeks(1).plusDays(1).plusHours(1).plusMinutes(1).plusMinutes(1);
        System.err.println("endTime : " + endTime);

        long years = ChronoUnit.YEARS.between(startTime, endTime);
        System.err.println("日 "+years);
        long months = ChronoUnit.MONTHS.between(startTime,endTime);
        System.err.println("月 "+months);
        long weeks = ChronoUnit.WEEKS.between(startTime,endTime);
        System.err.println("周 "+weeks);
        long days = ChronoUnit.DAYS.between(startTime,endTime);
        System.err.println("日 "+days);
        long hours = ChronoUnit.HOURS.between(startTime,endTime);
        System.err.println("时 "+hours);
        long minutes = ChronoUnit.MINUTES.between(startTime,endTime);
        System.err.println("分 "+minutes);
        long seconds = ChronoUnit.SECONDS.between(startTime,endTime);
        System.err.println("秒 "+seconds);
        long millis = ChronoUnit.MILLIS.between(startTime,endTime);
        System.err.println("月 "+months);
        System.err.printf("时间间隔 : %d 年 %d 月 %d 周 %d 日 %d 时 %d 分 %d 秒 %d 毫秒 ", years,months,weeks,days,hours,minutes,seconds,millis);
    }
运行结果:
startTime : 2022-05-12T17:57:05.379
endTime : 2023-06-20T18:59:05.380
日 1
月 13
周 57
日 404
时 9697
分 581822
秒 34909320
月 13
时间间隔 : 1 年 13 月 57 周 404 日 9697 时 581822 分 34909320 秒 34909320001 毫秒 

五.Until

until同四.ChronoUnit类一样,计算有年、月、周、日、时、分、秒、毫秒

计算的是LocalDate和LocalDateTime两个时间分别间隔的年、月、周、日、时、分、秒、毫秒

public static void main(String[] args) {
        LocalDateTime startTime = LocalDateTime.now();
        System.err.println("startTime : " + startTime);
        LocalDateTime endTime = LocalDateTime.now().plusYears(1).plusMonths(1).plusWeeks(1).plusDays(1).plusHours(1).plusMinutes(1).plusMinutes(1);
        System.err.println("endTime : " + endTime);

        long years = startTime.until(endTime, ChronoUnit.YEARS);
        System.err.println("日 "+years);
        long months = startTime.until(endTime, ChronoUnit.MONTHS);
        System.err.println("月 "+months);
        long weeks = startTime.until(endTime, ChronoUnit.WEEKS);
        System.err.println("周 "+weeks);
        long days = startTime.until(endTime, ChronoUnit.DAYS);
        System.err.println("日 "+days);
        long hours = startTime.until(endTime, ChronoUnit.HOURS);
        System.err.println("时 "+hours);
        long minutes = startTime.until(endTime, ChronoUnit.MINUTES);
        System.err.println("分 "+minutes);
        long seconds = startTime.until(endTime, ChronoUnit.SECONDS);
        System.err.println("秒 "+seconds);
        long millis = startTime.until(endTime, ChronoUnit.MILLIS);
        System.err.println("月 "+months);
        System.err.printf("时间间隔 : %d 年 %d 月 %d 周 %d 日 %d 时 %d 分 %d 秒 %d 毫秒 ", years,months,weeks,days,hours,minutes,seconds,millis);
    }
运行结果:
startTime : 2022-05-12T18:01:45.622
endTime : 2023-06-20T19:03:45.623
日 1
月 13
周 57
日 404
时 9697
分 581822
秒 34909320
月 13
时间间隔 : 1 年 13 月 57 周 404 日 9697 时 581822 分 34909320 秒 34909320001 毫秒 

六.计算LocalDateTime两个时间间隔的日、时、分、秒

   public static void main(String[] args) {
        LocalDateTime startTime = LocalDateTime.now();
        System.err.println("startTime : " + startTime);
        LocalDateTime endTime = LocalDateTime.now().plusYears(0).plusMonths(1).plusWeeks(1).plusDays(1).plusHours(1).plusMinutes(1).plusMinutes(1);
        System.err.println("endTime : " + endTime);

        long between = Duration.between(startTime, endTime).getSeconds();

        long days = between / 60 /60 / 24;
        System.err.println("日 "+days);
        long hours = between / 60 / 60 % 24;
        System.err.println("时 "+hours);
        long minutes = between / 60 % 60;
        System.err.println("分 "+minutes);
        long seconds = between % 60;
        System.err.println("秒 "+seconds);
        System.err.printf("时间间隔 : %d 日 %d 时 %d 分 %d 秒 %d 毫秒 ", days,hours,minutes,seconds);
    }
运行结果:
startTime : 2022-05-12T20:04:42.435
endTime : 2022-06-20T21:06:42.435
日 39
时 1
分 2
秒 0
时间间隔 : 39 日 1 时 2 分 0 秒 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值