MonthDay類的isValidYear()方法用於檢查指定的年份是否對此month-day有效。此方法隻能在2月29日返回false。
用法:
public boolean isValidYear?(int year)
參數:此方法接受一個參數年份,即要驗證的年份。
返回值:如果該month-day的年份有效,則此方法返回布爾值true。
以下示例程序旨在說明isValidYear()方法:
示例1:
// 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
示例2:
// 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://docs.oracle.com/javase/10/docs/api/java/time/MonthDay.html#isValidYear(int)
相關用法
- Java MonthDay with()用法及代碼示例
- Java MonthDay get()用法及代碼示例
- Java MonthDay from()用法及代碼示例
- Java MonthDay isBefore()用法及代碼示例
- Java MonthDay isAfter()用法及代碼示例
- Java MonthDay getLong()用法及代碼示例
- Java MonthDay format()用法及代碼示例
- Java MonthDay equals()用法及代碼示例
- Java MonthDay getDayOfMonth()用法及代碼示例
- Java MonthDay compareTo()用法及代碼示例
- Java MonthDay atYear()用法及代碼示例
- Java MonthDay isSupported()用法及代碼示例
- Java MonthDay getMonth()用法及代碼示例
- Java MonthDay hashCode()用法及代碼示例
- Java MonthDay getMonthValue()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 MonthDay isValidYear() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。