栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

一.简述

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/876797.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号