持續時間類是JavaFX的一部分。 Duration類定義了一段時間。 Duration類是不可變的,因此將其替換而不是修改。有兩種方法可以創建Duration類:
- 使用構造函數
- 使用諸如秒(double)或分鍾(double)的靜態構造方法之一
該類的構造函數:
- Duration(double m):使用指定的持續時間(以毫秒為單位)創建一個新的Duration對象。
常用方法:
方法 | 說明 |
---|---|
add(Duration d) | 將持續時間d添加到持續時間對象 |
subtract(Duration d) | 將持續時間d減去持續時間對象 |
divide(Duration d) | 將持續時間d除以持續時間對象 |
multiply(Duration d) | 將持續時間d乘以持續時間對象 |
millis(double m) | 返回具有指定持續時間(以毫秒為單位)的持續時間對象 |
seconds(double m) | 返回具有指定持續時間(以秒為單位)的持續時間對象 |
minutes(double m) | 返回具有指定持續時間(以分鍾為單位)的持續時間對象 |
hours(double m) | 返回具有指定持續時間(以小時為單位)的持續時間對象 |
toMillis() | 返回此時間段內的毫秒數 |
toSeconds() | 返回此期間的秒數 |
toMinutes() | 返回此期間的分鍾數 |
toHours() | 返回此時段的小時數 |
lessThan(Duration d) | 如果指定的持續時間小於此實例的(<),則返回true。 |
greaterThan(Duration d) | 如果指定的持續時間大於(>)此實例,則返回true。 |
greaterThanOrEqualTo(Duration d) | 如果指定的持續時間大於或等於此實例(> =),則返回true。 |
lessThanOrEqualTo(Duration d) | 如果指定的持續時間小於或等於此實例的(<=),則返回true。 |
以下示例程序旨在說明Duration類的用法:
- Java程序創建兩個Duration對象,並以毫秒,秒,分鍾和小時顯示時間:該程序創建兩個名為Duration_1和duration_2的Duration對象。毫秒數作為參數傳遞。調用display函數,並將duration對象作為參數傳遞。使用函數toHours(),toMinutes(),toSeconds(),toMillis()將持續時間轉換為小時,分鍾,秒和毫秒。並顯示結果。
// Java Program to create two Duration objects and // display the time in milliseconds seconds, // minutes and hours import javafx.util.Duration; import java.util.*; class Duration_1 { // Main Method public static void main(String args[]) { try { // duration object Duration duration_1 = new Duration(1000000); Duration duration_2 = new Duration(100012); // display the duration System.out.println("Duration 1"); display(duration_1); System.out.println(""); System.out.println("Duration 2"); display(duration_2); } catch (Exception e) { System.err.println(e.getMessage()); } } // display function public static void display(Duration duration) { // display the details of a duration System.out.println("Duartion in miliseconds:" + duration.toMillis()); System.out.println("Duartion in seconds:" + duration.toSeconds()); System.out.println("Duartion in minutes:" + duration.toMinutes()); System.out.println("Duartion in hours:" + duration.toHours()); } }
輸出:
Duration 1 Duartion in miliseconds:1000000.0 Duartion in seconds:1000.0 Duartion in minutes:16.666666666666668 Duartion in hours:0.2777777777777778 Duration 2 Duartion in miliseconds:100012.0 Duartion in seconds:100.012 Duartion in minutes:1.6668666666666667 Duartion in hours:0.02778111111111111
- Java程序創建四個Duration對象並顯示它們,並添加兩個對象,將兩個對象相乘,除以兩個對象,然後減去兩個對象並顯示結果:該程序創建四個名為Duration_1,duration_2,duration_3和duration_4的Duration對象。調用display函數,並將duration對象作為參數傳遞。使用函數toHours(),toMinutes(),toSeconds(),toMillis()將持續時間轉換為小時,分鍾,秒和毫秒。並顯示結果。將duration_1和duration_2相加和相減,並顯示結果對象。類似地,duration_3和duration_4被除以並相乘,並顯示結果對象。
// Java program to create four Duration object and // display them and add two objects, multiply two // objects, divide two objects and subtract two // objects and display the results. import javafx.util.Duration; import java.util.*; class Duration_2 { // Main Method public static void main(String args[]) { try { // duration object Duration duration_1 = Duration.millis(1000000); Duration duration_2 = Duration.seconds(100012); Duration duration_3 = Duration.hours(18912); Duration duration_4 = Duration.minutes(45634); // display the duration System.out.println("\nDuration 1\n"); display(duration_1); System.out.println("\nDuration 2\n"); display(duration_2); System.out.println("\nDuration 3\n"); display(duration_3); System.out.println("\nDuration 4\n"); display(duration_4); System.out.println("\nAdd duration 1 and duration 2\n"); display(duration_1.add(duration_2)); System.out.println("\nDivide duration 3 and duration 4\n"); display(duration_3.divide(duration_4)); System.out.println("\nMutiply duration 3 and duration 4\n"); display(duration_3.multiply(duration_4)); System.out.println("\nSubstract duration 1 and duration 2\n"); display(duration_1.subtract(duration_2)); } catch (Exception e) { System.err.println(e.getMessage()); } } // display method public static void display(Duration duration) { // display the details of a duration System.out.println("Duartion in miliseconds:" + duration.toMillis()); System.out.println("Duartion in seconds:" + duration.toSeconds()); System.out.println("Duartion in minutes:" + duration.toMinutes()); System.out.println("Duartion in hours:" + duration.toHours()); } }
輸出:
Duration 1 Duartion in miliseconds:1000000.0 Duartion in seconds:1000.0 Duartion in minutes:16.666666666666668 Duartion in hours:0.2777777777777778 Duration 2 Duartion in miliseconds:1.00012E8 Duartion in seconds:100012.0 Duartion in minutes:1666.8666666666666 Duartion in hours:27.781111111111112 Duration 3 Duartion in miliseconds:6.80832E10 Duartion in seconds:6.80832E7 Duartion in minutes:1134720.0 Duartion in hours:18912.0 Duration 4 Duartion in miliseconds:2.73804E9 Duartion in seconds:2738040.0 Duartion in minutes:45634.0 Duartion in hours:760.5666666666667 Add duration 1 and duration 2 Duartion in miliseconds:1.01012E8 Duartion in seconds:101012.0 Duartion in minutes:1683.5333333333333 Duartion in hours:28.058888888888887 Divide duration 3 and duration 4 Duartion in miliseconds:24.86567033352325 Duartion in seconds:0.02486567033352325 Duartion in minutes:4.1442783889205417E-4 Duartion in hours:6.907130648200903E-6 Mutiply duration 3 and duration 4 Duartion in miliseconds:1.86414524928E20 Duartion in seconds:1.86414524928E17 Duartion in minutes:3.1069087488E15 Duartion in hours:5.178181248E13 Substract duration 1 and duration 2 Duartion in miliseconds:-9.9012E7 Duartion in seconds:-99012.0 Duartion in minutes:-1650.2 Duartion in hours:-27.503333333333334
注意:以上程序可能無法在在線IDE中運行。請使用離線編譯器。
參考: https://docs.oracle.com/javase/8/javafx/api/javafx/util/Duration.html
相關用法
- JavaFX 類 Pos用法及代碼示例
- JavaFX 類 Tab用法及代碼示例
- JavaFX 類 ToolBar用法及代碼示例
- JavaFX 類 Background用法及代碼示例
- JavaFX 類 WebView用法及代碼示例
- JavaFX 類 VBox用法及代碼示例
- JavaFX 類 HBox用法及代碼示例
- JavaFX 類 BorderPane用法及代碼示例
- JavaFX 類 Popup用法及代碼示例
- JavaFX 類 ToggleButton用法及代碼示例
- JavaFX 類 Rectangle2D用法及代碼示例
- JavaFX 類 ColorInput用法及代碼示例
- JavaFX 類 Hyperlink用法及代碼示例
- JavaFX 類 Slider用法及代碼示例
- JavaFX 類 TabPane用法及代碼示例
注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 JavaFX | Duration Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。