可以使用equals()Java中Duration类中的方法确定两个持续时间的相等性。该方法需要一个参数,即要比较的持续时间。同样,如果两个持续时间相等,则返回true,否则返回false。
演示此的程序如下所示-
import java.time.Duration;
public class GFG {
public static void main(String[] args) {
Duration d1 = Duration.ofDays(1);
Duration d2 = Duration.ofHours(24);
boolean flag = d1.equals(d2);
if(flag)
System.out.println("The durations are equal");
else
System.out.println("The durations are not equal");
}
}输出结果
The durations are equal
现在让我们了解上面的程序。
使用方法比较两个持续时间equals(),并将返回值存储在标志中。然后显示持续时间是否相等。演示这的代码片段如下-
Duration d1 = Duration.ofDays(1);
Duration d2 = Duration.ofHours(24);
boolean flag = d1.equals(d2);
if(flag)
System.out.println("The durations are equal");
else
System.out.println("The durations are not equal");