您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“Java的Duration類如何使用”,內容詳細,步驟清晰,細節處理妥當,希望這篇“Java的Duration類如何使用”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
說明
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新增的。
Period用法
見:詳解Java中Period類的使用方法
基于天、時、分、秒、納秒創建。
ofDays(), ofHours(), ofMillis(), ofMinutes(), ofNanos(), ofSeconds()。例如:
Duration fromDays = Duration.ofDays(1);
通過LocalDateTime或者LocalTime 類,然后使用between獲取創建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 du1 = Duration.ofHours(10); Duration duration = Duration.from(du1);
用法示例
Duration fromChar1 = Duration.parse("P1DT1H10M10.5S"); Duration fromChar2 = Duration.parse("PT10M");
格式說明
采用ISO-8601時間格式。格式為:PnYnMnDTnHnMnS (n為個數)
例如:P1Y2M10DT2H30M15.03S
P:開始標記
1Y:一年
2M:兩個月
10D:十天
T:日期和時間的分割標記
2H:兩個小時
30M:三十分鐘
15S:15.02秒
1."P", "D", "H", "M" 和 "S"可以是大寫或者小寫(建議大寫)
2.可以用“-”表示負數
示例大全
"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) { ...... } }
比較兩個時間的差
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); // 任何一個時間單元為負數,則返回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 類型參數進行加減:
Duration duration = Duration.ofHours(2); Duration newDuration = duration.plus(33, ChronoUnit.SECONDS);
可以用toX來轉換為其他單位,支持: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來獲得指定位置的值,因為Duration是由秒和納秒組成,所以只能獲得秒和納秒:
Duration duration = Duration.ofHours(2); duration.getSeconds(); //7200 duration.getNano(); //
讀到這里,這篇“Java的Duration類如何使用”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。