JavaFX |持续时间类
Duration 类是 JavaFX 的一部分。 Duration 类定义了一段时间。 Duration 类是不可变的,因此它被替换而不是修改。创建 Duration 类有两种方法:
- 使用构造函数
- 使用一种静态构造方法,例如 seconds(double) 或 minutes(double)
类的构造函数:
- Duration(double m) :创建一个具有指定持续时间(以毫秒为单位)的新 Duration 对象。
常用方法:Method Explanation add(Duration d) Adds the duration d to the duration object subtract(Duration d) Subtracts the duration d to the duration object divide(Duration d) Divides the duration d to the duration object multiply(Duration d) Multiplies the duration d to the duration object millis(double m) Returns a duration object with specified duration in milliseconds seconds(double m) Returns a duration object with specified duration in seconds minutes(double m) Returns a duration object with specified duration in minutes hours(double m) Returns a duration object with specified duration in hours toMillis() Returns the number of milliseconds in this period toSeconds() Returns the number of seconds in this period toMinutes() Returns the number of minutes in this period toHours() Returns the number of hours in this period lessThan(Duration d) Returns true if the specified duration is less than (<) this instance. greaterThan(Duration d) Returns true if the specified duration is greater than (>) this instance. greaterThanOrEqualTo(Duration d) Returns true if the specified duration is greater than or equal to (>=) this instance. lessThanOrEqualTo(Duration d) Returns true if the specified duration is less than or equal to (<=) this instance.
下面的程序说明了 Duration 类的使用:
- Java程序创建两个 Duration 对象并以毫秒、秒、分钟和小时为单位显示时间:该程序创建两个名为duration_1和duration_2的 Duration 对象。毫秒数作为参数传递。调用显示函数并将持续时间对象作为参数传递。使用函数toHours() 、 toMinutes() 、 toSeconds() 、 toMillis()将持续时间转换为小时、分钟、秒和毫秒,并显示结果。
Java
// 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("Duration in milliseconds: "
+ duration.toMillis());
System.out.println("Duration in seconds: "
+ duration.toSeconds());
System.out.println("Duration in minutes: "
+ duration.toMinutes());
System.out.println("Duration in hours: "
+ duration.toHours());
}
}
Java
// 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("\nMultiply duration 3 and duration 4\n");
display(duration_3.multiply(duration_4));
System.out.println("\nSubtract 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("Duration in milliseconds: "
+ duration.toMillis());
System.out.println("Duration in seconds: "
+ duration.toSeconds());
System.out.println("Duration in minutes: "
+ duration.toMinutes());
System.out.println("Duration in hours: "
+ duration.toHours());
}
}
输出:
Duration 1
Duration in milliseconds: 1000000.0
Duration in seconds: 1000.0
Duration in minutes: 16.666666666666668
Duration in hours: 0.2777777777777778
Duration 2
Duration in milliseconds: 100012.0
Duration in seconds: 100.012
Duration in minutes: 1.6668666666666667
Duration in hours: 0.02778111111111111
- Java程序创建四个 Duration 对象并显示它们并添加两个对象,乘两个对象,除以两个对象并减去两个对象并显示结果:该程序创建四个 Duration 对象,分别命名为duration_1 、 duration_2 、 duration_3和duration_4 。调用显示函数并将持续时间对象作为参数传递。使用函数toHours() 、 toMinutes() 、 toSeconds() 、 toMillis()将持续时间转换为小时、分钟、秒和毫秒,并显示结果。 duration_1和duration_2相加和相减并显示结果对象。类似地, duration_3和duration_4相除并相乘,显示结果对象。
Java
// 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("\nMultiply duration 3 and duration 4\n");
display(duration_3.multiply(duration_4));
System.out.println("\nSubtract 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("Duration in milliseconds: "
+ duration.toMillis());
System.out.println("Duration in seconds: "
+ duration.toSeconds());
System.out.println("Duration in minutes: "
+ duration.toMinutes());
System.out.println("Duration in hours: "
+ duration.toHours());
}
}
输出:
Duration 1
Duration in milliseconds: 1000000.0
Duration in seconds: 1000.0
Duration in minutes: 16.666666666666668
Duration in hours: 0.2777777777777778
Duration 2
Duration in milliseconds: 1.00012E8
Duration in seconds: 100012.0
Duration in minutes: 1666.8666666666666
Duration in hours: 27.781111111111112
Duration 3
Duration in milliseconds: 6.80832E10
Duration in seconds: 6.80832E7
Duration in minutes: 1134720.0
Duration in hours: 18912.0
Duration 4
Duration in milliseconds: 2.73804E9
Duration in seconds: 2738040.0
Duration in minutes: 45634.0
Duration in hours: 760.5666666666667
Add duration 1 and duration 2
Duration in milliseconds: 1.01012E8
Duration in seconds: 101012.0
Duration in minutes: 1683.5333333333333
Duration in hours: 28.058888888888887
Divide duration 3 and duration 4
Duration in milliseconds: 24.86567033352325
Duration in seconds: 0.02486567033352325
Duration in minutes: 4.1442783889205417E-4
Duration in hours: 6.907130648200903E-6
Multiply duration 3 and duration 4
Duration in milliseconds: 1.86414524928E20
Duration in seconds: 1.86414524928E17
Duration in minutes: 3.1069087488E15
Duration in hours: 5.178181248E13
Subtract duration 1 and duration 2
Duration in milliseconds: -9.9012E7
Duration in seconds: -99012.0
Duration in minutes: -1650.2
Duration in hours: -27.503333333333334
注意:上述程序可能无法在在线 IDE 中运行。请使用离线编译器。
参考: https://docs.oracle.com/javase/8/javafx/api/javafx/util/Duration.html