Java中的持续时间isNegative()方法
可以使用isNegative()Java中Duration类中的方法来检查持续时间是否为负。此方法不需要任何参数。此外,如果持续时间为负数,则返回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");热门推荐
10 新年给妈妈简短祝福语
11 生日祝福语舅舅 简短独特
12 毕业结婚祝福语简短精辟
13 宝宝祝福语诗意简短 古文
14 牙医生日祝福语简短
15 猪年给老师祝福语简短
16 鱼的内涵简短祝福语
17 2026祝福语简短创意牛
18 贺卡文字同事祝福语简短