Java Duration equals(Duration)方法及实例

Java Duration equals(Duration)方法及实例

java.time包中Duration类的equals(Duration)方法用于检查该期限是否等于作为参数传递的期限。

语法。

public boolean equals(Duration otherDuration)

参数。该方法接受一个参数otherDuration,该参数是将与之比较的持续时间。

返回值。该方法返回一个布尔值,显示该Duration是否等于其他Duration。

下面的例子说明了Duration.equals()方法。

例1:

// Java code to illustrate equals() method
  
import java.time.Duration;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Duration using parse() method
        Duration duration1
            = Duration.parse("P2DT3H4M");
  
        // Duration using ofDays() method
        Duration duration2
            = Duration.ofDays(10);
  
        // Compare the durations
        // using equals() method
        System.out.println(duration1
                               .equals(duration2));
    }
}

输出:

false

例2:

// Java code to illustrate equals() method
  
import java.time.Duration;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Duration using ofHours() method
        Duration duration1
            = Duration.ofHours(5);
  
        // Duration using ofHours() method
        Duration duration2
            = Duration.ofHours(5);
  
        // Compare the durations
        // using equals() method
        System.out.println(duration1
                               .equals(duration2));
    }
}

输出:

true

例3:

// Java code to illustrate equals() method
  
import java.time.Duration;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Duration using ofDays() method
        Duration duration1
            = Duration.ofDays(5);
  
        // Duration using ofHours() method
        Duration duration2
            = Duration.ofHours(5);
  
        // Compare the durations
        // using equals() method
        System.out.println(duration1
                               .equals(duration2));
    }
}

输出:

false

参考资料。甲骨文文件

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程