Java ChronoPeriod isZero()方法及实例

来源:这里教程网 时间:2026-02-17 20:41:14 作者:

Java ChronoPeriod isZero()方法及实例

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

为了检查零期,这个函数被广泛使用。零周期是指年、月、日这三个时间段都为零的周期。

语法

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.*;import java.time.chrono.*;import java.time.temporal.ChronoUnit;  public class ChronoPeriodDemo {      // Function to check if all    // of the three YEAR, MONTH, DAY are zero    static void ifZero(int year, int months, int days)    {        ChronoPeriod 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.*;import java.time.chrono.*;import java.time.temporal.ChronoUnit;  public class ChronoPeriodDemo {      // Function to check if all    // of the three YEAR, MONTH, DAY are zero    static void ifZero(int year, int months, int days)    {        ChronoPeriod 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/9/docs/api/java/time/chrono/ChronoPeriod.html#isZero-

相关推荐