Java时间系列(JDK8)--Period的使用

编程入门 行业动态 更新时间:2024-10-17 13:29:14

原文网址:Java时间系列(JDK8)--Period的使用_IT利刃出鞘的博客-CSDN博客

简介

本文用示例介绍java的Period的用法。

Duration和Period

说明

        Duration类通过年月日时分秒相结合来描述一个时间量,最高精度是纳秒。时间量可以为正也可以为负,比如1天(86400秒0纳秒)、-1天(-86400秒0纳秒)、1年(31556952秒0纳秒)、1毫秒(0秒1000000纳秒)等。

        Period类通过年月日相结合来描述一个时间量,最高精度是。时间量可以为正也可以为负,例如2年(2年0个月0天)、3个月(0年3个月0天)、4天(0年0月4天)等。

        这两个类是不可变的、线程安全的、最终类。都是JDK8新增的。

Duration用法

见:Java--Duration--使用/实例_IT利刃出鞘的博客-CSDN博客

创建方法

通过时间单位创建

如果仅一个值表示,如使用ofDays()方法,那么其他值为0。

若仅用ofWeeks,则其天数为week数乘以7.

Period fromUnits = Period.of(3, 10, 10);
Period fromDays = Period.ofDays(50);
Period fromMonths = Period.ofMonths(5);
Period fromYears = Period.ofYears(10);
Period fromWeeks = Period.ofWeeks(40);  //280天

通过LocalDate创建

LocalDate startDate = LocalDate.of(2015, 2, 20);
LocalDate endDate = LocalDate.of(2017, 1, 15);
// startDate减endDate
Period period = Period.between(startDate, endDate);

解析方法

格式1:“PnYnMnWnD”

  • P:开始符,表示period(即:表示年月日);
  • Y:year;
  • M:month;
  • W:week;
  • D:day

P, Y, M, W, D都可以用大写或者小写。

Period period = Period.parse("P2Y");       //2年
Period period = Period.parse("P2Y3M5D");   //2年3月5天
Period period = Period.parse("P1Y2M3W4D"); // 1年2月3周4天。即:1年2月25天

源码

public final class Period
        implements ChronoPeriod, Serializable {
    //-----------------------------------------------------------------------
    /**
     * Obtains a {@code Period} from a text string such as {@code PnYnMnD}.
     * <p>
     * This will parse the string produced by {@code toString()} which is
     * based on the ISO-8601 period formats {@code PnYnMnD} and {@code PnW}.
     * <p>
     * The string starts with an optional sign, denoted by the ASCII negative
     * or positive symbol. If negative, the whole period is negated.
     * The ASCII letter "P" is next in upper or lower case.
     * There are then four sections, each consisting of a number and a suffix.
     * At least one of the four sections must be present.
     * The sections have suffixes in ASCII of "Y", "M", "W" and "D" for
     * years, months, weeks and days, accepted in upper or lower case.
     * The suffixes must occur in order.
     * The number part of each section must consist of ASCII digits.
     * The number may be prefixed by the ASCII negative or positive symbol.
     * The number must parse to an {@code int}.
     * <p>
     * The leading plus/minus sign, and negative values for other units are
     * not part of the ISO-8601 standard. In addition, ISO-8601 does not
     * permit mixing between the {@code PnYnMnD} and {@code PnW} formats.
     * Any week-based input is multiplied by 7 and treated as a number of days.
     * <p>
     * For example, the following are valid inputs:
     * <pre>
     *   "P2Y"             -- Period.ofYears(2)
     *   "P3M"             -- Period.ofMonths(3)
     *   "P4W"             -- Period.ofWeeks(4)
     *   "P5D"             -- Period.ofDays(5)
     *   "P1Y2M3D"         -- Period.of(1, 2, 3)
     *   "P1Y2M3W4D"       -- Period.of(1, 2, 25)
     *   "P-1Y2M"          -- Period.of(-1, 2, 0)
     *   "-P1Y2M"          -- Period.of(-1, -2, 0)
     * </pre>
     *
     * @param text  the text to parse, not null
     * @return the parsed period, not null
     * @throws DateTimeParseException if the text cannot be parsed to a period
     */
    public static Period parse(CharSequence text) {
        // 其他代码
    }

    // 其他代码
}

获得年月日

period.getYears();
period.getMonths();
period.getDays();

比较方法

用between来比较日期。

LocalDate startDate = LocalDate.of(2015, 2, 20);
LocalDate endDate = LocalDate.of(2017, 1, 15);
// startDate减endDate
Period period = Period.between(startDate, endDate);
// 任何一个时间单元为负数,则返回true。true:endDate早于startDate
period.isNegative()

增减方法

Period period = Period.parse("P2Y3M5D");
period.plusDays(50);
period.minusMonths(2);

转换单位

Period period = Period.parse("P1Y2M3D");
period.toTotalMonths(); // 14

取值方法

Period period = Period.parse("P1Y2M3D");
period.getYears();  // 1
period.getMonths(); // 2
period.getDays();   // 3

其他网址

介绍java 8 的 Period 和 Duration 类_一次次失望后的平静-CSDN博客

更多推荐

Java时间系列(JDK8)--Period的使用

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

发布评论

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

>www.elefans.com

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