当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。