最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Java中Period類的使用方法

 更新時間:2022年05月24日 09:50:51   作者:IT利刃出鞘  
Period類通過年、月、日相結合來描述一個時間量,最高精度是天。本文將通過示例詳細為大家講講Period類的使用,需要的可以參考一下

簡介

本文用示例介紹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類的使用方法

創(chuàng)建方法

通過時間單位創(chuàng)建

如果僅一個值表示,如使用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創(chuàng)建

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中Period類的使用方法的文章就介紹到這了,更多相關Java Period類內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java中線程組ThreadGroup與線程池的區(qū)別及示例

    Java中線程組ThreadGroup與線程池的區(qū)別及示例

    這篇文章主要介紹了Java中線程組與線程池的區(qū)別及示例,ThreadGroup是用來管理一組線程的,可以控制線程的執(zhí)行,查看線程的執(zhí)行狀態(tài)等操作,方便對于一組線程的統(tǒng)一管理,需要的朋友可以參考下
    2023-05-05
  • java對指定目錄下文件讀寫操作介紹

    java對指定目錄下文件讀寫操作介紹

    本文將詳細介紹java對指定目錄下文件的讀寫功能實現(xiàn),需要的朋友可以參考下
    2012-11-11
  • Spring如何在xml文件中配置Bean

    Spring如何在xml文件中配置Bean

    這篇文章主要介紹了Spring如何在xml文件中配置Bean的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • java 增強型for循環(huán)語法詳解

    java 增強型for循環(huán)語法詳解

    增強型 for 循環(huán)(也稱為 “for-each” 循環(huán))是 Java 從 JDK 5 開始引入的一種便捷循環(huán)語法,旨在簡化對數組或集合類的迭代操作,這篇文章主要介紹了java 增強型for循環(huán) 詳解,需要的朋友可以參考下
    2025-04-04
  • 詳談Lock與synchronized 的區(qū)別

    詳談Lock與synchronized 的區(qū)別

    下面小編就為大家?guī)硪黄斦凩ock與synchronized 的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • java線程池參數自定義設置詳解

    java線程池參數自定義設置詳解

    這篇文章主要為大家介紹了java線程池參數自定義設置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • Spring中@PostConstruct注解的使用講解

    Spring中@PostConstruct注解的使用講解

    這篇文章主要介紹了Spring中@PostConstruct注解的使用講解,被@PostConstruct修飾的方法會在服務器加載Servlet的時候運行,并且只會被服務器執(zhí)行一次,PostConstruct在構造函數之后執(zhí)行,init()方法之前執(zhí)行,PreDestroy()方法在destroy()方法之后執(zhí)行,需要的朋友可以參考下
    2023-11-11
  • Java導入新項目報錯java:JDK?isn‘t?specified?for?module解決辦法

    Java導入新項目報錯java:JDK?isn‘t?specified?for?module解決辦法

    這篇文章主要給大家介紹了關于Java導入新項目報錯java:JDK?isn‘t?specified?for?module的解決辦法,當您在導入Java項目時遇到錯誤時,可以嘗試以下面的方法來處理,需要的朋友可以參考下
    2024-05-05
  • 自己動手寫一個java版簡單云相冊

    自己動手寫一個java版簡單云相冊

    這篇文章主要為大家分享了自己動手寫的一個java版簡單云相冊,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • SpringBoot項目啟動過程動態(tài)修改接口請求路徑的解決方案

    SpringBoot項目啟動過程動態(tài)修改接口請求路徑的解決方案

    在SpringBoot服務整合過程中,遇到了多個服務中相同RequestMapping路徑導致的啟動問題,解決方案是通過修改RequestMappingHandlerMapping類的getMappingForMethod方法,本文給大家介紹SpringBoot修改接口請求路徑的解決方案,感興趣的朋友一起看看吧
    2024-09-09

最新評論

烟台市| 安福县| 同德县| 奇台县| 始兴县| 南宁市| 遵化市| 电白县| 高雄县| 迁西县| 武山县| 嘉义市| 固原市| 大竹县| 甘肃省| 会东县| 科技| 昔阳县| 夏河县| 玛纳斯县| 丰城市| 来宾市| 寿宁县| 颍上县| 顺平县| 新营市| 洪江市| 呼图壁县| 泗阳县| 青龙| 渑池县| 泰来县| 莱州市| 襄樊市| 长春市| 宝应县| 沅陵县| 灌阳县| 合肥市| 崇义县| 来凤县|