admin管理员组

文章数量:1595877

一.简述

在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 秒 

本文标签: 四种区别方式时间Duration