JavaFX | Duration Class

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:

Method Explantion
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:

  1. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    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
    

  2. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    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
    

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



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.