Java Period isZero()方法及示例

来源:这里教程网 时间:2026-02-17 21:05:39 作者:

Java Period isZero()方法及示例

Java中Period类的isZero()方法用于检查这个周期中的年、月、日三者是否都是零。

为了检查零周期,这个函数被广泛使用。零周期是指年、月、日三者全部为零的周期。

语法

public boolean isZero()

参数: 该方法不接受任何参数。

返回值: 如果给定时间段的YEARS、MONTHS、DAYS三个值都是0,则该方法返回一个布尔值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

参考资料 : https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#isZero-

相关推荐