Java 中的 Duration isZero() 方法
可以通过 Java 中 Duration 类的 isZero() 方法检查持续时间是否为零长度。此方法不需要任何参数。而且,如果持续时间为零长度,则返回 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() 方法的返回值。根据 flag 中的值,持续时间的长度为零还是不是,并将其打印出来。一个展示此功能的代码片段如下 −
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");
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP