可以使用isZero()Java的Duration类中的方法来检查持续时间是否为零长度。此方法不需要任何参数。同样,如果持续时间长度为零,则返回true,否则返回false。
演示此的程序如下所示-
import java.time.Duration;
public class Demo {
public static void main(String[] args) {
Duration d = Duration.ofHours(1);
boolean flag = d.isZero();
System.out.println("The duration is: " + d);
if(flag)
System.out.println("The above duration is of zero length");
else
System.out.println("The above duration is not of zero length");
}
}输出结果
The duration is: PT1H The above duration is not of zero length
现在让我们了解上面的程序。
首先显示持续时间。然后flag保存isZero()方法的返回值。根据标志中的值,持续时间的长度是否为零,并进行打印。演示这的代码片段如下-
Duration d = Duration.ofHours(1);
boolean flag = d.isZero();
System.out.println("The duration is: " + d);
if(flag)
System.out.println("The above duration is of zero length");
else
System.out.println("The above duration is not of zero length");