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 秒
版权声明:本文标题:Java8中计算时间的四种方式及区别Period、Duration、ChronoUnit、Until 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1728236707a1150501.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论