Duration isNegative() 方法在 Java 中
在 Java 的 Duration 类中使用 isNegative() 方法可以检查持续时间是否为负。此方法不需要任何参数。此外,如果持续时间为负,它返回 true,否则返回 false。
演示这一点的一个程序如下 −
示例
import java.time.Duration;
public class Demo {
public static void main(String[] args) {
Duration d = Duration.ofSeconds(5);
boolean flag = d.isNegative();
System.out.println("The duration is: " + d);
if(flag)
System.out.println("The above duration is negative");
else
System.out.println("The above duration is not negative");
}
}输出
The duration is: PT5S The above duration is not negative
现在让我们了解一下上面的程序。
首先,显示持续时间。然后,标志保存 isNegative() 方法的返回值。根据标志中的值,持续时间是否为负或者不为负并被打印出来。一个展示这一点的代码片段如下 −
Duration d = Duration.ofSeconds(5);
boolean flag = d.isNegative();
System.out.println("The duration is: " + d);
if(flag)
System.out.println("The above duration is negative");
else
System.out.println("The above duration is not negative");
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP