当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JavaFX 类 Duration用法及代码示例


持续时间类是JavaFX的一部分。 Duration类定义了一段时间。 Duration类是不可变的,因此将其替换而不是修改。有两种方法可以创建Duration类:

  1. 使用构造函数
  2. 使用诸如秒(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类的用法:

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



相关用法


注:本文由纯净天空筛选整理自andrew1234大神的英文原创作品 JavaFX | Duration Class。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。