Java year isBefore()方法及示例

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

Java year isBefore()方法及示例

Java中Year类的isBefore()方法用于检查当前的Year对象是否在作为该方法参数指定的Year之前。

语法:

public boolean isBefore(Year otherYear)

参数 :它接受一个单一的参数otherYear,当前的Year对象将被与之比较。

返回值 :如果这个Year对象的值在作为该方法参数指定的Year对象的值之前,它返回一个布尔值True,否则它返回False。

以下程序说明了Java中Year的isBefore()方法:
程序1 :

// Program to illustrate the isBefore() method  import java.util.*;import java.time.*;  public class GfG {    public static void main(String[] args)    {        // Create first Year object        Year firstYear = Year.of(2018);          // Create second Year object        Year secondYear = Year.of(1997);          // Check if this year object's value is        // before the specified Year or not        System.out.println(firstYear.isBefore(secondYear));    }}

输出:

false

程序2 :

// Program to illustrate the isBefore() method  import java.util.*;import java.time.*;  public class GfG {    public static void main(String[] args)    {        // Create first Year object        Year firstYear = Year.of(1997);          // Create second Year object        Year secondYear = Year.of(2018);          // Check if this year object's value is        // before the specified Year or not        System.out.println(firstYear.isBefore(secondYear));    }}

输出:

true

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

相关推荐