📜  Java中的 MonthDay isValidYear() 方法及示例

📅  最后修改于: 2022-05-13 01:54:46.640000             🧑  作者: Mango

Java中的 MonthDay isValidYear() 方法及示例

MonthDay类的isValidYear()方法,用于检查指定年份对于本月日是否有效。这只能在 2 月 29 日返回 false。

句法:

public boolean isValidYear?(int year)

参数:此方法接受一个参数year ,即要验证的年份。

返回值:如果年份对本月日有效,则此方法返回布尔值 true。

下面的程序说明了 isValidYear() 方法:
方案一:

// Java program to demonstrate
// MonthDay.isValidYear() method
  
import java.time.*;
import java.time.temporal.ChronoField;
  
public class GFG {
    public static void main(String[] args)
    {
        // create a MonthDay object
        MonthDay month = MonthDay.parse("--10-12");
  
        // apply isValidYear() method
        boolean value = month.isValidYear(2012);
  
        // print result
        System.out.println("Year 2012 is valid for monthday: "
                           + month + " = " + value);
    }
}
输出:
Year 2012 is valid for monthday: --10-12 = true

方案二:

// Java program to demonstrate
// MonthDay.isValidYear() method
  
import java.time.*;
import java.time.temporal.ChronoField;
  
public class GFG {
    public static void main(String[] args)
    {
        // create a MonthDay object
        MonthDay month = MonthDay.parse("--02-29");
  
        // apply isValidYear() method
        boolean value = month.isValidYear(2017);
  
        // print result
        System.out.println("Year 2017 is valid for monthday: "
                           + month + " = " + value);
    }
}
输出:
Year 2017 is valid for monthday: --02-29 = false

参考资料: https: Java/time/MonthDay.html#isValidYear(int)