欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Java中Period類的使用方法

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

簡(jiǎn)介

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

Duration和Period

說明

Duration類通過秒和納秒相結(jié)合來描述一個(gè)時(shí)間量,最高精度是納秒。時(shí)間量可以為正也可以為負(fù),比如1天(86400秒0納秒)、-1天(-86400秒0納秒)、1年(31556952秒0納秒)、1毫秒(0秒1000000納秒)等。

Period類通過年、月、日相結(jié)合來描述一個(gè)時(shí)間量,最高精度是天。時(shí)間量可以為正也可以為負(fù),例如2年(2年0個(gè)月0天)、3個(gè)月(0年3個(gè)月0天)、4天(0年0月4天)等。

這兩個(gè)類是不可變的、線程安全的、最終類。都是JDK8新增的。

Duration用法

見:詳解Java中Duration類的使用方法

創(chuàng)建方法

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

如果僅一個(gè)值表示,如使用ofDays()方法,那么其他值為0。

若僅用ofWeeks,則其天數(shù)為week數(shù)乘以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);
// 任何一個(gè)時(shí)間單元為負(fù)數(shù),則返回true。true:endDate早于startDate
period.isNegative()

增減方法

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

轉(zhuǎn)換單位

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

取值方法

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

到此這篇關(guān)于詳解Java中Period類的使用方法的文章就介紹到這了,更多相關(guān)Java Period類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JAVA實(shí)現(xiàn)Excel和PDF上下標(biāo)的操作代碼

    JAVA實(shí)現(xiàn)Excel和PDF上下標(biāo)的操作代碼

    這篇文章主要介紹了JAVA實(shí)現(xiàn)Excel和PDF上下標(biāo),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • Spring Boot 工程的創(chuàng)建和運(yùn)行(圖文)

    Spring Boot 工程的創(chuàng)建和運(yùn)行(圖文)

    這篇文章主要介紹了Spring Boot 工程的創(chuàng)建和運(yùn)行(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-02-02
  • SpringBoot整合Dubbo zookeeper過程解析

    SpringBoot整合Dubbo zookeeper過程解析

    這篇文章主要介紹了SpringBoot整合Dubbo zookeeper過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • 一文搞懂Java中的序列化與反序列化

    一文搞懂Java中的序列化與反序列化

    序列化是將對(duì)象轉(zhuǎn)換成二進(jìn)制字節(jié)流的過程;反序列化是從二進(jìn)制字節(jié)流中恢復(fù)對(duì)象的過程。文中降通過示例詳解二者的使用與區(qū)別,需要的可以參考一下
    2022-08-08
  • Spring中的Context你真的懂了嗎

    Spring中的Context你真的懂了嗎

    這篇文章主要給大家介紹了關(guān)于Spring中Context的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • HttpClient實(shí)現(xiàn)表單提交上傳文件

    HttpClient實(shí)現(xiàn)表單提交上傳文件

    這篇文章主要為大家詳細(xì)介紹了HttpClient實(shí)現(xiàn)表單提交上傳文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • java顯示當(dāng)前的系統(tǒng)時(shí)間

    java顯示當(dāng)前的系統(tǒng)時(shí)間

    這篇文章主要介紹了java如何顯示當(dāng)前的系統(tǒng)時(shí)間,代碼很簡(jiǎn)單,自己可以自定義顯示的系統(tǒng)時(shí)間的顏色和字體,需要的朋友可以參考下
    2015-10-10
  • Springboot Cache @CacheEvict 無法模糊刪除的解決方案

    Springboot Cache @CacheEvict 無法模糊刪除的解決方案

    這篇文章主要介紹了Springboot Cache @CacheEvict 無法模糊刪除的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java enum關(guān)鍵字不識(shí)別的快速解決辦法

    Java enum關(guān)鍵字不識(shí)別的快速解決辦法

    這篇文章主要介紹了Java enum關(guān)鍵字不識(shí)別的快速解決辦法,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧
    2016-09-09
  • Java中MapStruct映射處理器報(bào)錯(cuò)的問題解決

    Java中MapStruct映射處理器報(bào)錯(cuò)的問題解決

    MapStruct是一個(gè)強(qiáng)大的Java映射框架,它能夠在編譯時(shí)生成映射代碼,,本文主要介紹了Java中MapStruct映射處理器報(bào)錯(cuò)的問題解決,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03

最新評(píng)論