Java ChronoPeriod isNegative()方法及实例
Java中 ChronoPeriod类 的 isNegative()方法 用于检查周期中的YEAR、MONTH、DAYS三者是否为负数。
语法
boolean isNegative()
参数: 该方法不接受任何参数。
返回值: 如果给定时期的年、月、日三个值中的任何一个为负值,该方法返回True,否则将返回false。
以下程序说明了Java中的isNegative()方法。
程序1 :
// Java code to show the function isNegative()
// to check whether any of the three given units
// YEAR, MONTH, DAY is negative
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
public class ChronoPeriodDemo {
// Function to check if any of the three
// YEAR, MONTH, DAY is negative
static void ifNegative(int year, int months, int days)
{
ChronoPeriod period = Period.of(year, months, days);
System.out.println(period.isNegative());
}
// Driver Code
public static void main(String[] args)
{
int year = -12;
int months = 3;
int days = 31;
ifNegative(year, months, days);
}
}
输出:
true
程序2 :
// Java code to show the function isNegative()
// to check whether any of the three given units
// YEAR, MONTH, DAY is negative
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
public class ChronoPeriodDemo {
// Function to check if any of the three
// YEAR, MONTH, DAY is negative
static void ifNegative(int year, int months, int days)
{
ChronoPeriod period = Period.of(year, months, days);
System.out.println(period.isNegative());
}
// Driver Code
public static void main(String[] args)
{
int year = 12;
int months = 3;
int days = 31;
ifNegative(year, months, days);
}
}
输出:
false
参考资料 : https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoPeriod.html#isNegative-