Duration:用于计算两个“时间”间隔
简介:
用法:
1、Zero常量
实例:
1 Duration duration = Duration.ZERO; 2 System.out.println("持续时间常量值:"+duration.toNanos()); 3 System.out.println("持续时间常量值是否为零:"+duration.isZero()); 4 System.out.println("持续时间常量值是正数:"+duration.abs().toNanos());
2、ofDays
实例:
1 Duration durationOfDays = Duration.ofDays(1); 2 System.out.println("一天有多少小时:"+durationOfDays.toHours()+"H");
3、ofHours
实例:
1 Duration durationOfHours = Duration.ofHours(2); 2 System.out.println("一小时有多少分:"+durationOfHours.toMinutes()+"m");
4、ofMinutes
实例:
1 Duration durationOfMinutes = Duration.ofMinutes(1); 2 System.out.println("一分钟有多少秒:"+durationOfMinutes.getSeconds()+"s");
5、ofSeconds
实例:
1 Duration durationOfSeconds = Duration.ofSeconds(1); 2 System.out.println("一秒有多少秒:"+durationOfSeconds.getSeconds()+"s");
实例:
1 Duration durationOfSecondsNano = Duration.ofSeconds(2,2000); 2 System.out.println("2000纳秒有多少秒:"+durationOfSecondsNano.getSeconds()+"s");
6、ofMillis
实例:
1 Duration durationOfMillis = Duration.ofMillis(5000); 2 System.out.println("5000纳秒有多少秒:"+durationOfMillis.getSeconds()+"s");
7、ofNanos
实例:
1 Duration durationOfNanos = Duration.ofNanos(10000); 2 System.out.println("10000纳秒有多少纳秒:"+durationOfNanos.toNanos()+"s");
8、of
实例:
1 Duration durationOf = Duration.of(10,SECONDS); 2 System.out.println("10秒有多少秒:"+durationOf.getSeconds()+"s");
9、from
实例:
1 Duration durationFrom = Duration.from(Duration.ofDays(10)); 2 System.out.println("10天有多少小时:"+durationFrom.toHours()+"H");
10、parse
实例:
1 Duration durationParse = Duration.parse("PT15M"); 2 System.out.println("格式转化15分钟:"+durationParse.toMinutes()+"M");
11、between
实例:
1 Duration durationBetween = Duration.between(LocalTime.NOON,LocalTime.MAX); 2 System.out.println("距离今天结束还有多少时间:"+durationBetween.toHours()+"时"+durationBetween.toMinutes()+"分"+durationBetween.getSeconds()+"秒");