📜  Java中的 Duration equals(Duration) 方法及示例

📅  最后修改于: 2022-05-13 01:55:16.200000             🧑  作者: Mango

Java中的 Duration equals(Duration) 方法及示例

Java包中的Duration类equals(Duration)方法用于检查这个duration是否等于作为参数传入的duration。

句法:

public boolean equals(Duration otherDuration)

参数:此方法接受参数otherDuration ,该参数是与此持续时间进行比较的持续时间。

返回值:此方法返回一个布尔值,显示此 Duration 是否等于 otherDuration。

下面的示例说明了 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

参考: Oracle 文档