Java中的Period类的isZero()方法用于检查此周期中的三个YEAR,MONTH,DAYS是否全部为零。
为了检查零周期,该功能被广泛使用。零期间是YEAR,MONTHS和DAYS这三个时间均为零的期间。
用法:
public boolean isZero()
参数:此方法不接受任何参数。
返回值:如果给定时间段的YEARS,MONTHS,DAYS这三个值全部为零,则此方法返回布尔值True,否则将返回False。
以下示例程序旨在说明上述方法:
程序1:
// Java code to show the function isZero()
// to check whether all of the three given units
// YEAR, MONTH, DAY are zero.
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class PeriodDemo {
// Function to check if all of the three YEAR, MONTH, DAY are zero
static void ifZero(int year, int months, int days)
{
Period period = Period.of(year, months, days);
System.out.println(period.isZero());
}
// Driver Code
public static void main(String[] args)
{
int year = 0;
int months = 0;
int days = 0;
ifZero(year, months, days);
}
}
输出:
true
程序2:
// Java code to show the function isZero()
// to check whether all of the three given units
// YEAR, MONTH, DAY are zero.
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class PeriodDemo {
// Function to check if all of the three YEAR, MONTH, DAY are zero
static void ifZero(int year, int months, int days)
{
Period period = Period.of(year, months, days);
System.out.println(period.isZero());
}
// Driver Code
public static void main(String[] args)
{
int year = 0;
int months = 0;
int days = -12;
ifZero(year, months, days);
}
}
输出:
false