java中的Duration是一個用來測量以秒和奈秒為單位的時間的類別。 java中duration類別的套件是java.time.Duration。 Duration 類別物件指定時間段或用於確定兩個時間之間的差異。 Duration 物件是不可變的且是線程安全的,因為 Duration 物件是不可變的,因此一旦創建它,我們就無法更改它的值。但是,我們可以基於另一個 Duration 物件建立新的 Duration 物件。 Duration 類別繼承了一個物件類別(因為物件是 java 中所有類別的超類別)並實作了 Comparable 介面。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
文法
java中Duration類別宣告的語法是:
public final class Duration extends Object implements Comparable < Duration >, TemporalAmount, Serializable { // variables and method of the class Duration}
Duration 類別方法清單在下面透過範例程式碼進行了解釋;範例程式碼可以進一步用於類似的方法(對於未給出的每個方法範例程式碼):
我們透過下面的範例java程式碼來理解上述方法。
代碼:
package p1; import java.time.Duration; import java.time.LocalTime; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.between(LocalTime.MAX,LocalTime.MIN); System.out.println(d.get(ChronoUnit.SECONDS)); Duration absd = d.abs(); System.out.println(absd.get(ChronoUnit.SECONDS)); } }
輸出:
我們透過下面的範例java程式碼來理解上述方法:
代碼:
package p1; import java.time.Duration; import java.time.*; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.between(LocalTime.MAX,LocalTime.MIN); LocalDateTime date = LocalDateTime.now(); System.out.println(date); date = (LocalDateTime)d.addTo(date); System.out.println(date); Duration d1 = d.dividedBy(4); System.out.println(d1); System.out.println(d.getSeconds()); System.out.println(d1.getSeconds()); } }
輸出:
我們透過下面的範例java程式碼來理解上述方法:
代碼:
package p1; import java.time.Duration; import java.time.*; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.between(LocalTime.NOON,LocalTime.MAX); Duration d1 = Duration.between(LocalTime.NOON,LocalTime.MIN); System.out.println(d1.getSeconds()); System.out.println(d.compareTo(d1)); System.out.println(d1.compareTo(d1)); System.out.println(d1.compareTo(d)); System.out.println(d1.equals(d)); System.out.println(d1.isNegative()); System.out.println(d1.isZero()); } }
輸出:
我們透過下面的範例java程式碼來理解上述方法:
代碼:
package p1; import java.time.Duration; import java.time.*; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.from(ChronoUnit.DAYS.getDuration()); System.out.println(d.toMinutes()); } }
輸出:
我們透過下面的範例java程式碼來理解上述方法:
代碼:
package p1; import java.time.Duration; import java.time.*; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.between(LocalTime.NOON,LocalTime.MAX); System.out.println(d.getUnits()); System.out.println(d.toMinutes()); System.out.println(d.getSeconds()); System.out.println(d.getNano()); System.out.println(d.getClass()); } }
輸出:
We understand the above methods with the below sample java code:
Code:
package p1; import java.time.Duration; import java.time.*; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.ofDays(6); System.out.println(d.getSeconds()); Duration d1 = d.minusDays(3); System.out.println(d1.getSeconds()); d = Duration.ofHours(6); System.out.println(d.getSeconds()); d1 = d.minusHours(2); System.out.println(d1.getSeconds()); } }
Output:
We understand the above methods with the below sample java code:
Code:
package p1; import java.time.Duration; import java.time.*; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.ofDays(6); System.out.println(d.getSeconds()); Duration d1 = d.plusDays(2); System.out.println(d1.getSeconds()); d = Duration.ofHours(6); System.out.println(d.getSeconds()); d1 = d.plusHours(2); System.out.println(d1.getSeconds()); } }
Output:
We understand the above methods with the below sample java code:
Code:
package p1; import java.time.Duration; import java.time.*; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.ofDays(6); System.out.println(d.toHours()); Duration d1 =Duration.ofHours(24) ; System.out.println(d1.toDays()); } }
Output:
We understand the above methods with the below sample java code:
Code:
package p1; import java.time.Duration; import java.time.*; import java.time.temporal.ChronoUnit; public class DurationClassDemo { public static void main(String[] args) { Duration d = Duration.ofDays(6); System.out.println(d.toString()); d = d.withSeconds(3000); System.out.println(d.toString()); } }
Output:
The Duration class is one of the built-in class in java, which is used to measure time in seconds and nanoseconds and add, subtract, and convert the duration, or, in simple words, the duration class allows performance operation on time or day duration. The duration class is available in java.time.Duration package of java.
以上是Java 持續時間的詳細內容。更多資訊請關注PHP中文網其他相關文章!