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

詳解Java中Duration類的使用方法

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

簡(jiǎn)介

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

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新增的。

Period用法

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

創(chuàng)建方法

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

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

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

Duration fromDays = Duration.ofDays(1);

通過LocalDateTime或LocalTime

通過LocalDateTime或者LocalTime 類,然后使用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);

通過已有的Duration

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

解析方法

用法說明

用法示例

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

格式說明

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

例如:P1Y2M10DT2H30M15.03S

P:開始標(biāo)記

1Y:一年

2M:兩個(gè)月

10D:十天

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

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

30M:三十分鐘

15S:15.02秒

詳解

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

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 類型參數(shù)進(jìn)行加減:

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

轉(zhuǎn)換單位

可以用toX來轉(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來獲得指定位置的值,因?yàn)镈uration是由秒和納秒組成,所以只能獲得秒和納秒:

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

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

相關(guān)文章

最新評(píng)論

黄石市| 阳城县| 和平区| 丹江口市| 南和县| 广安市| 平邑县| 神农架林区| 江西省| 开封县| 正镶白旗| 星座| 鲁甸县| 阳曲县| 惠来县| 南京市| 涟源市| 环江| 宁南县| 乐平市| 观塘区| 龙泉市| 叶城县| 仙桃市| 曲周县| 怀来县| 光山县| 黎城县| 桃江县| 沙坪坝区| 容城县| 安图县| 新乐市| 逊克县| 江阴市| 耿马| 阿坝| 岳普湖县| 武清区| 海南省| 寻乌县|