Java中的Period isNegative()方法
可以使用isNegative()Java中Period类中的方法来检查Period中的天,月和年是否为负。此方法不需要任何参数。此外,如果期间中的任何天,月和年均为负,则返回true,如果期间中的所有天,月和年均为正,则返回false。
演示此程序如下
示例
import java.time.Period;
public class Demo {
public static void main(String[] args) {
String period = "P5Y9M4D";
Period p = Period.parse(period);
System.out.println("The Period is: " + p);
System.out.println("The days, months and years in the Period are negative? " + p.isNegative());
}
}输出结果
The Period is: P5Y9M4D The days, months and years in the Period are negative? false
现在让我们了解上面的程序。
首先显示期间。然后使用该isNegative()方法检查期间中的天,月和年是否为负,并显示返回值。演示此代码段如下所示:
String period = "P5Y9M4D";
Period p = Period.parse(period);
System.out.println("The Period is: " + p);
System.out.println("The days, months and years in the Period are negative? " +
p.isNegative());