當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java MonthDay isValidYear()用法及代碼示例


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)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 MonthDay isValidYear() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。