Java8中计算时间的四种方式及区别Period、Duration、ChronoUnit、Until & 时间区间Duration的简单使用

一.简述

在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
时间间隔 : 160

三.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.426125150290120
毫秒90120000
时间间隔 : 12515029012090120000 毫秒 

四.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.3801135740496975818223490932013
时间间隔 : 1135740496975818223490932034909320001 毫秒 

五.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.6231135740496975818223490932013
时间间隔 : 1135740496975818223490932034909320001 毫秒 

六.计算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.43539120
时间间隔 : 39120



创建时间区间Duration的一些方法

简单介绍一下时间区间类Duration的一些常用方法,方便计算时间区间和时间段的一些处理。
jdk1.8及更高版本可用。

1、between

传入两个时间计算时间区间
示例:

LocalDateTime start = LocalDateTime.of(2022,1,1,8,0,0);
LocalDateTime end = LocalDateTime.of(2022,1,2,8,30,30);
Duration duration = Duration.between(start, end);

2、parse(“PnDTnHnMn.nS”)

示例:

        Duration duration = Duration.parse("PnDTnHnMn.nS");

parse参数的格式PaDTbHcMd.eS:a天b小时c分钟d.e秒
源码例子:

        Examples:
          "PT20.345S" -- parses as "20.345 seconds"
          "PT15M"     -- parses as "15 minutes" (where a minute is 60 seconds)
          "PT10H"     -- parses as "10 hours" (where an hour is 3600 seconds)
          "P2D"       -- parses as "2 days" (where a day is 24 hours or 86400 seconds)
          "P2DT3H4M"  -- parses as "2 days, 3 hours and 4 minutes"
          "P-6H3M"    -- parses as "-6 hours and +3 minutes"
          "-P6H3M"    -- parses as "-6 hours and -3 minutes"
          "-P-6H+3M"  -- parses as "+6 hours and -3 minutes"

3、Duration.of(long amount, TemporalUnit unit)

示例:

        Duration duration = Duration.of(2, ChronoUnit.DAYS);

参数TemporalUnit unit详见ChronoUnit枚举类

    //纳秒
    NANOS("Nanos", Duration.ofNanos(1)),
    //微秒
    MICROS("Micros", Duration.ofNanos(1000)),
    //毫秒
    MILLIS("Millis", Duration.ofNanos(1000_000)),
    //秒
    SECONDS("Seconds", Duration.ofSeconds(1)),
    //分
    MINUTES("Minutes", Duration.ofSeconds(60)),
    //小时
    HOURS("Hours", Duration.ofSeconds(3600)),
    //半天
    HALF_DAYS("HalfDays", Duration.ofSeconds(43200)),
    //天
    DAYS("Days", Duration.ofSeconds(86400)),
    //周
    WEEKS("Weeks", Duration.ofSeconds(7 * 86400L)),
    //月
    MONTHS("Months", Duration.ofSeconds(31556952L / 12)),
    //年
    YEARS("Years", Duration.ofSeconds(31556952L)),
    //十年
    DECADES("Decades", Duration.ofSeconds(31556952L * 10L)),
    //世纪(百年)
    CENTURIES("Centuries", Duration.ofSeconds(31556952L * 100L)),
    //千年
    MILLENNIA("Millennia", Duration.ofSeconds(31556952L * 1000L)),
    //纪元(好多年)
    ERAS("Eras", Duration.ofSeconds(31556952L * 1000_000_000L)),
    
    FOREVER("Forever", Duration.ofSeconds(Long.MAX_VALUE, 999_999_999));

4、Duration.from

示例:

        Duration du1 = Duration.ofHours(10);
		Duration duration = Duration.from(du1);

5、Duration.ofDays(long days)

创建时间区间 Duration.ofDays(long days)
类似方法:ofDays、ofHours、ofMinutes、ofSeconds、ofMillis、ofNanos

		Duration duration = Duration.ofDays(2);

时间区间计算的一些方法

区间加减运算:plus minus (plusDays、plusHours、plusMinutes、plusSeconds、plusMillis、plusNanos)

		duration = duration.plus(Duration.ofDays(2));
		duration = duration.minus(Duration.ofDays(1));
		duration = duration.plusDays(2);
		duration = duration.minusDays(1);

示例

import java.time.Duration;
import java.time.LocalDateTime;

	public static void main(String[] args) {
		LocalDateTime start = LocalDateTime.of(2022,1,1,8,0,0);
		System.out.println("计算两个时间的差:");
		LocalDateTime end = LocalDateTime.of(2022,1,2,8,30,30);
		//创建时间区间 between
 		//Duration duration = Duration.between(start, end);

		//创建时间区间 parse("PnDTnHnMn.nS")
        //Duration duration = Duration.parse("PT15M");

		//创建时间区间 from
		//Duration du1 = Duration.ofHours(10);
		//Duration duration = Duration.from(du1);

		//创建时间区间 Duration.of(long amount, TemporalUnit unit);unit 详见ChronoUnit枚举类
		//Duration duration = Duration.of(2, ChronoUnit.DAYS);

		//创建时间区间 Duration.ofDays(long days)
		Duration duration = Duration.ofDays(2);

		//区间加减运算:plus minus (days、hours、Minutes、Seconds、Millis、Nanos)
		duration = duration.plus(Duration.ofDays(2));
		duration = duration.minus(Duration.ofDays(1));
		duration = duration.plusDays(2);
		duration = duration.minusDays(1);

		long days = duration.toDays();
		System.out.println("相差:"+days+"天");
		long hours = duration.toHours();
		System.out.println("相差:"+hours+"小时");
		long minutes = duration.toMinutes();
		System.out.println("相差:"+minutes+"分");
		long millis = duration.toMillis();
		System.out.println("相差:"+millis+"毫秒");
		long nanos = duration.toNanos();
		System.out.println("相差:"+nanos+"纳秒");
	}

输出结果:
Java8中计算时间的四种方式及区别Period、Duration、ChronoUnit、Until & 时间区间Duration的简单使用_第1张图片

你可能感兴趣的:(Java8新特性,java,时间,Period,Duration,ChronoUnit)