具有示例的java.time.Duration类
时间:2020-01-09 10:35:17 来源:igfitidea点击:
java.time.Duration类是Java 8中添加的新日期和时间API的一部分,该API表示基于时间的时间量,例如" 34.5秒"。
持续时间类以秒和纳秒为单位对时间量或者时间量进行建模。可以使用其他基于持续时间的单位(例如分钟和小时)来访问它。此外,可以使用DAYS单位并将其视为完全等于24小时,因此忽略了夏时制的影响。
由于Duration类以秒和纳秒为单位对时间进行建模,因此它具有两个字段,其中一个字段长代表秒,一个int代表纳秒,始终在0到999,999,999之间。
Duration类中的of()方法
我们可以使用以下方法获取Duration类的实例:
- of(long amount, TemporalUnit unit)
- ofDays(long days)
- ofHours(long hours)
- ofMillis(long millis)
- ofMinutes(long minutes)
- ofNanos(long nanos)
- ofSeconds(long seconds)
- ofSeconds(long seconds, long nanoAdjustment)
public class DurationExample { public static void main(String[] args) { // Using days- stored as hours Duration duration = Duration.ofDays(122); System.out.println("Duration in days- " + duration); // Using hours- stored as hours duration = Duration.ofHours(150); System.out.println("Duration in hours- " + duration); // Using minutes- stored as minutes duration = Duration.ofMinutes(220); System.out.println("Duration in Minutes- " + duration); // Using Seconds duration = Duration.ofSeconds(12); System.out.println("Duration in Seconds- " + duration); // Using milliseconds- stored as Seconds duration = Duration.ofMillis(11); System.out.println("Duration in Millis- " + duration); // Using nanoseconds- stored as Seconds duration = Duration.ofNanos(240000); System.out.println("Duration in Nanos- " + duration); // Using Seconds and nanoseconds duration = Duration.ofSeconds(1200, 245); System.out.println("Duration in Seconds and NanoSeconds- " + duration); } }
输出:
Duration in days- PT2928H Duration in hours- PT150H Duration in Minutes- PT3H40M Duration in Seconds- PT12S Duration in Millis- PT0.011S Duration in Nanos- PT0.00024S Duration in Seconds and NanoSeconds- PT20M0.000000245S
使用ween()方法查找两个本地时间之间的差异
Java Duration类中有一个between方法
- 之间(时间开始包含在内,时间结束包含在内)
使用它可以获得两次之间的小时,分钟和秒数的持续时间。
public class DurationExample { public static void main(String[] args) { LocalTime t1 = LocalTime.of(5, 30, 56); LocalTime t2 = LocalTime.of(15, 35, 45); System.out.println("Start Time- " + t1); System.out.println("End Time- " + t2); Duration duration = Duration.between(t1, t2); // Total duration in hours System.out.println("Hours between- " + duration.toHours()); // Total duration in Minutes System.out.println("Total difference in Minutes- " + duration.toMinutes()); // Total duration in Seconds System.out.println("Total difference in Seconds- " + duration.toSeconds()); // Difference between two times System.out.println("Hours between " + duration.toHours()); System.out.println("Minutes between " + duration.toMinutesPart()); System.out.println("Seconds between " + duration.toSecondsPart()); } }
输出:
Start Time- 05:30:56 End Time- 15:35:45 Hours between 10 Minutes between 604 Seconds between 36289 Hours between 10 Minutes between 4 Seconds between 49
在这里,请注意toMinutes()和toMinutesPart()方法之间的区别,以及toSeconds()和toSecondsPart()方法之间的区别。
toMinutes()–获取此持续时间的分钟数。
toMinutesPart()–提取持续时间中分钟的分钟数。
toSeconds()–获取此持续时间的秒数。
toSecondsPart()–提取持续时间的秒数。
从给定时间开始的正负几天,小时,分钟,秒
使用plus(long amountToAdd,TemporalUnit单位)和minus(long amountToSubtract,TemporalUnit单位)方法可以从给定LocalTime中添加或者减去Duration。请注意,Duration类实现TemporalAmount。
public class DurationExample { public static void main(String[] args) { LocalTime lt = LocalTime.of(5, 30, 56); System.out.println("LocalTime- " + lt); System.out.println("Plus 2 hours " + lt.plus(Duration.ofHours(2))); System.out.println("Plus 5 Minutes " + lt.plus(Duration.ofMinutes(5))); System.out.println("Plus 20 Seconds " + lt.plus(Duration.ofSeconds(20))); } }
输出:
LocalTime- 05:30:56 Plus 2 hours 07:30:56 Plus 5 Minutes 05:35:56 Plus 20 Seconds 05:31:16
public class DurationExample { public static void main(String[] args) { LocalTime lt = LocalTime.of(5, 30, 56); System.out.println("LocalTime- " + lt); System.out.println("Minus 2 hours " + lt.minus(Duration.ofHours(2))); System.out.println("Minus 45 Minutes " + lt.minus(Duration.ofMinutes(45))); System.out.println("Minus 20 Seconds " + lt.minus(Duration.ofSeconds(20))); } }
输出:
LocalTime- 05:30:56 Minus 2 hours 03:30:56 Minus 45 Minutes 04:45:56 Minus 20 Seconds 05:30:36
将字符串转换为持续时间– parse()方法
- parse(CharSequence text)-从文本字符串(例如PnDTnHnMn.nS)获取持续时间。
在格式" PnDTnHnMn.nS"中,nD表示天数,T表示时间,nH表示小时数,nM表示分钟数,nS表示秒数。格式的字母接受大写或者小写。
public class DurationExample { public static void main(String[] args) { Duration duration = Duration.parse("PT20.345S"); System.out.println("Duration- " + duration); duration = Duration.parse("P2DT3H4M"); System.out.println("Duration- " + duration); duration = Duration.parse("PT-6H3M"); System.out.println("Duration- " + duration); } }
输出:
Duration- PT20.345S Duration- PT51H4M Duration- PT-5H-57M