Java 8 的 Period 和 Duration 类

编程入门 行业动态 更新时间:2024-10-21 16:24:16

Period 和 Duration。两个类看表示时间量或两个日期之间的差,两者之间的差异为:Period基于日期值,而Duration基于时间值。

1.Period 类

Period 类表示一段时间的年、月、日,开使用between()方法获取两个日期之间的差作为Period 对象返回:

        LocalDate startTime = LocalDate.of(2021, 12, 15);
        LocalDate endTime = LocalDate.of(2022, 1, 5);
        Period period = Period.between(startTime, endTime);
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
        System.out.println(period.isNegative());
0
0
21
false

如果isNegative()返回false,那么startDate早于endDate。

基于年、月、日和周创建Period类型对象

        Period fromUnits = Period.of(3, 10, 10);
        Period days = Period.ofDays(50);
        Period years = Period.ofYears(10);
        Period months = Period.ofMonths(5);
        Period weeks = Period.ofWeeks(10);
        System.out.println(fromUnits);
        System.out.println(days);
        System.out.println(years);
        System.out.println(months);
        System.out.println(weeks);

通过解析文本序列来创建Period,其格式为“PnYnMnD”: 

        int years = Period.parse("P3Y4M5D").getDays();
        System.out.println(years);

 period的值可以通过plusX()、minusX()方法进行增加或减少,其中X表示日期单元:

        int days = Period.parse("P3Y4M5D").plusDays(9).getDays();
        int days1 = Period.parse("P3Y4M5D").minusDays(6).getDays();
        System.out.println(days1);


2.Duration 类

Duration类表示秒或纳秒时间间隔,适合处理较短的时间,需要更高的精确性。我们能使用between()方法比较两个瞬间的差:

        Instant start = Instant.parse("2021-10-03T10:15:30.00Z");
        Instant end = Instant.parse("2021-10-03T10:16:30.00Z");

        Duration duration = Duration.between(start, end);
        System.out.println(duration.getSeconds());

通过LocalDateTime 类获取获取Duration对象: 

        LocalTime start = LocalTime.of(1, 20, 25, 1024);
        LocalTime end = LocalTime.of(3, 22, 27, 1544);

        long seconds = Duration.between(start, end).getSeconds();
        System.out.println(seconds);

 

        LocalTime start = LocalTime.of(1, 20, 25, 1024);
        LocalTime end = LocalTime.of(3, 22, 27, 1544);

        Duration duration = Duration.between(start, end);

        System.out.println(duration.isNegative());

基于下面的方法获得Duration对象,ofDays(), ofHours(), ofMillis(), ofMinutes(), ofNanos(), ofSeconds(): 

        Duration minutes = Duration.ofMinutes(1);
        System.out.println(minutes.getSeconds());

通过文本序列创建Duration对象,格式为 “PnDTnHnMn.nS”:

        Duration parse = Duration.parse("P1DT1H10M10.5S");
        System.out.println(parse.getSeconds());

 通过 plusX()、minusX()方法增加或减少Duration对象,其中X表示days, hours, millis, minutes, nanos 或 seconds

        Duration parse = Duration.parse("P1DT1H10M10.5S");
        System.out.println(parse.plusSeconds(5).getSeconds());

更多推荐

Java 8 的 Period 和 Duration 类

本文发布于:2023-06-13 23:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416071.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java   Period   Duration

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!