Skip to content
Related Articles

Related Articles

Improve Article

JavaFX | Duration Class

  • Last Updated : 13 Sep, 2021

Duration class is a part of JavaFX. The Duration class defines a period of time. The Duration class is immutable so it is replaced rather than modified. There are two ways to create a Duration class:  

  1. Using the constructor
  2. Using one of the static construction methods such as seconds(double) or minutes(double)

Constructors of the class: 

  • Duration(double m): Creates a new Duration object with specified duration in milliseconds.

Commonly Used Methods: 

MethodExplanation
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.

Below programs illustrate the use of Duration class:
 

  • Java Program to create two Duration objects and display the time in milliseconds seconds, minutes and hours: This Program creates two Duration objects named duration_1 and duration_2. The number of milliseconds is passed as arguments. The display function is called and the duration object is passed as arguments. The period of the duration is converted to hours, minutes, seconds and milliseconds using the function toHours(), toMinutes(), toSeconds(), toMillis() and the result is displayed. 

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());
    }
}

Output:

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 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: This Program creates four Duration objects named duration_1, duration_2, duration_3 and duration_4. The display function is called and the duration object is passed as arguments. The period of the duration is converted to hours, minutes, seconds and milliseconds using the function toHours(), toMinutes(), toSeconds(), toMillis() and the result is displayed. The duration_1 and duration_2 are added and subtracted and the resultant object is displayed. Similarly, the duration_3 and duration_4 are divided and multiplied and the resultant object is displayed. 
     

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());
    }
}

Output:

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

Substract 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

Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/util/Duration.html

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :