Java Period isNegative()方法及示例
Java中Period类的isNegative()方法用于检查周期中的YEAR, MONTH, DAYS三者是否为负数。
语法
public 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.Period;
import java.time.temporal.ChronoUnit;
public class PeriodDemo {
// Function to check if any of the three
// YEAR, MONTH, DAY is negative
static void ifNegative(int year, int months, int days)
{
Period 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.Period;
import java.time.temporal.ChronoUnit;
public class PeriodDemo {
// Function to check if any of the three
// YEAR, MONTH, DAY is negative
static void ifNegative(int year, int months, int days)
{
Period 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/8/docs/api/java/time/Period.html#isNegative-