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

詳解Java中Duration類(lèi)的使用方法

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

簡(jiǎn)介

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

Duration和Period

說(shuō)明

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

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

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

Period用法

見(jiàn):詳解Java中Period類(lèi)的使用方法

創(chuàng)建方法

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

基于天、時(shí)、分、秒、納秒創(chuàng)建。

ofDays(), ofHours(), ofMillis(), ofMinutes(), ofNanos(), ofSeconds()。例如:

Duration fromDays = Duration.ofDays(1);

通過(guò)LocalDateTime或LocalTime

通過(guò)LocalDateTime或者LocalTime 類(lèi),然后使用between獲取創(chuàng)建Duration。

LocalDateTime start = LocalDateTime.of(2022, 1, 1, 8, 0, 0);
LocalDateTime end = LocalDateTime.of(2022, 1, 2, 8, 30, 30);
 
Duration duration = Duration.between(start, end);

通過(guò)已有的Duration

Duration du1 = Duration.ofHours(10);
Duration duration = Duration.from(du1);

解析方法

用法說(shuō)明

用法示例

Duration fromChar1 = Duration.parse("P1DT1H10M10.5S");
Duration fromChar2 = Duration.parse("PT10M");

格式說(shuō)明

采用ISO-8601時(shí)間格式。格式為:PnYnMnDTnHnMnS   (n為個(gè)數(shù))

例如:P1Y2M10DT2H30M15.03S

P:開(kāi)始標(biāo)記

1Y:一年

2M:兩個(gè)月

10D:十天

T:日期和時(shí)間的分割標(biāo)記

2H:兩個(gè)小時(shí)

30M:三十分鐘

15S:15.02秒

詳解

1."P", "D", "H", "M" 和 "S"可以是大寫(xiě)或者小寫(xiě)(建議大寫(xiě))

2.可以用“-”表示負(fù)數(shù)

示例大全

"PT20.345S" -- parses as "20.345 seconds"
"PT15M"     -- parses as "15 minutes" (where a minute is 60 seconds)
"PT10H"     -- parses as "10 hours" (where an hour is 3600 seconds)
"P2D"       -- parses as "2 days" (where a day is 24 hours or 86400 seconds)
"P2DT3H4M"  -- parses as "2 days, 3 hours and 4 minutes"
"P-6H3M"    -- parses as "-6 hours and +3 minutes"
"-P6H3M"    -- parses as "-6 hours and -3 minutes"
"-P-6H+3M"  -- parses as "+6 hours and -3 minutes"

源碼:

public final class Duration
        implements TemporalAmount, Comparable<Duration>, Serializable {
		
	//其他代碼
	
    //-----------------------------------------------------------------------
    /**
     * Obtains a {@code Duration} from a text string such as {@code PnDTnHnMn.nS}.
     * <p>
     * This will parse a textual representation of a duration, including the
     * string produced by {@code toString()}. The formats accepted are based
     * on the ISO-8601 duration format {@code PnDTnHnMn.nS} with days
     * considered to be exactly 24 hours.
     * <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.
     * The sections have suffixes in ASCII of "D", "H", "M" and "S" for
     * days, hours, minutes and seconds, accepted in upper or lower case.
     * The suffixes must occur in order. The ASCII letter "T" must occur before
     * the first occurrence, if any, of an hour, minute or second section.
     * At least one of the four sections must be present, and if "T" is present
     * there must be at least one section after the "T".
     * The number part of each section must consist of one or more ASCII digits.
     * The number may be prefixed by the ASCII negative or positive symbol.
     * The number of days, hours and minutes must parse to an {@code long}.
     * The number of seconds must parse to an {@code long} with optional fraction.
     * The decimal point may be either a dot or a comma.
     * The fractional part may have from zero to 9 digits.
     * <p>
     * The leading plus/minus sign, and negative values for other units are
     * not part of the ISO-8601 standard.
     * <p>
     * Examples:
     * <pre>
     *    "PT20.345S" -- parses as "20.345 seconds"
     *    "PT15M"     -- parses as "15 minutes" (where a minute is 60 seconds)
     *    "PT10H"     -- parses as "10 hours" (where an hour is 3600 seconds)
     *    "P2D"       -- parses as "2 days" (where a day is 24 hours or 86400 seconds)
     *    "P2DT3H4M"  -- parses as "2 days, 3 hours and 4 minutes"
     *    "P-6H3M"    -- parses as "-6 hours and +3 minutes"
     *    "-P6H3M"    -- parses as "-6 hours and -3 minutes"
     *    "-P-6H+3M"  -- parses as "+6 hours and -3 minutes"
     * </pre>
     *
     * @param text  the text to parse, not null
     * @return the parsed duration, not null
     * @throws DateTimeParseException if the text cannot be parsed to a duration
     */
    public static Duration parse(CharSequence text) {
		......
	}
}

比較方法

比較兩個(gè)時(shí)間的差 

Instant start = Instant.parse("2017-10-03T10:15:30.00Z");
Instant end = Instant.parse("2017-10-03T10:16:30.00Z");
 
// start - end         
Duration duration = Duration.between(start, end);
 
// 任何一個(gè)時(shí)間單元為負(fù)數(shù),則返回true。true:end早于start
duration.isNegative();
 
Duration.between(start, end).getSeconds();
Duration.between(start, end).getNano();

增減方法

plusX()、minusX()

X表示days, hours, millis, minutes, nanos 或 seconds

Duration duration = Duration.ofHours(2);
Duration newDuration = duration.plusSeconds(33);

plus()/minus()方法

帶TemporalUnit 類(lèi)型參數(shù)進(jìn)行加減:

Duration duration = Duration.ofHours(2);
Duration newDuration = duration.plus(33, ChronoUnit.SECONDS);

轉(zhuǎn)換單位

可以用toX來(lái)轉(zhuǎn)換為其他單位,支持:toDays, toHours, toMinutes, toMillis, toNanos

Duration duration = Duration.ofHours(2);
 
duration.toDays();     // 0
duration.toHours();    // 2
duration.toMinutes();  // 120
duration.toMillis();   // 7200000
duration.toNanos();    // 7200000000000

取值方法

可以用getX來(lái)獲得指定位置的值,因?yàn)镈uration是由秒和納秒組成,所以只能獲得秒和納秒:

Duration duration = Duration.ofHours(2);
 
duration.getSeconds();  //7200
duration.getNano();     //

以上就是詳解Java中Duration類(lèi)的使用方法的詳細(xì)內(nèi)容,更多關(guān)于Java Duration類(lèi)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java基礎(chǔ)之CardLayout的使用

    Java基礎(chǔ)之CardLayout的使用

    這篇文章主要介紹了Java基礎(chǔ)之CardLayout的使用,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • java繪制五子棋棋盤(pán)

    java繪制五子棋棋盤(pán)

    這篇文章主要為大家詳細(xì)介紹了java繪制五子棋棋盤(pán),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • Springboot Caffeine本地緩存使用示例

    Springboot Caffeine本地緩存使用示例

    這篇文章主要介紹了Springboot Caffeine本地緩存使用示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Springboot自動(dòng)配置與@Configuration配置類(lèi)詳解

    Springboot自動(dòng)配置與@Configuration配置類(lèi)詳解

    這篇文章主要介紹了SpringBoot中的@Configuration與自動(dòng)配置,在進(jìn)行項(xiàng)目編寫(xiě)前,我們還需要知道一個(gè)東西,就是SpringBoot對(duì)我們的SpringMVC還做了哪些配置,包括如何擴(kuò)展,如何定制,只有把這些都搞清楚了,我們?cè)谥笫褂貌艜?huì)更加得心應(yīng)手
    2022-07-07
  • Python爬蟲(chóng) 12306搶票開(kāi)源代碼過(guò)程詳解

    Python爬蟲(chóng) 12306搶票開(kāi)源代碼過(guò)程詳解

    這篇文章主要介紹了Python爬蟲(chóng) 12306搶票開(kāi)源代碼過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • 淺談Java 代理機(jī)制

    淺談Java 代理機(jī)制

    Java 有兩種代理方式,一種是靜態(tài)代理,另一種是動(dòng)態(tài)代理。如果我們?cè)诖a編譯時(shí)就確定了被代理的類(lèi)是哪一個(gè),那么就可以直接使用靜態(tài)代理;如果不能確定,那么可以使用類(lèi)的動(dòng)態(tài)加載機(jī)制,在代碼運(yùn)行期間加載被代理的類(lèi)這就是動(dòng)態(tài)代理
    2021-06-06
  • java實(shí)現(xiàn)通用分頁(yè)(后端)

    java實(shí)現(xiàn)通用分頁(yè)(后端)

    這篇文章主要介紹了java實(shí)現(xiàn)通用分頁(yè)(后端)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • 關(guān)于Java中的try-with-resources語(yǔ)句

    關(guān)于Java中的try-with-resources語(yǔ)句

    這篇文章主要介紹了關(guān)于Java中的try-with-resources語(yǔ)句,try-with-resources是Java中的環(huán)繞語(yǔ)句之一,旨在減輕開(kāi)發(fā)人員釋放try塊中使用的資源的義務(wù),需要的朋友可以參考下
    2023-05-05
  • java實(shí)現(xiàn)簡(jiǎn)易聊天功能

    java實(shí)現(xiàn)簡(jiǎn)易聊天功能

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)易聊天功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • 使用@Value 注入 List 類(lèi)型的配置屬性需要注意的 BUG

    使用@Value 注入 List 類(lèi)型的配置屬性需要注意的 BUG

    這篇文章主要介紹了使用@Value 注入 List 類(lèi)型的配置屬性需要注意的 BUG,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08

最新評(píng)論