Sunday, January 25, 2026
HomeLanguagesJavaMonthDay isValidYear() Method in Java with Examples

MonthDay isValidYear() Method in Java with Examples

isValidYear() method of the MonthDay class used to check if the specified year is valid for this month-day.This can only return false for February 29th. Syntax:

public boolean isValidYear?(int year)

Parameters: This method accepts one parameter year which is the year to validate. Return value: This method returns boolean value true if the year is valid for this month-day. Below programs illustrate the isValidYear() method: Program 1: 

Java




// 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);
    }
}


Output:

Year 2012 is valid for monthday: --10-12 = true

Program 2: 

Java




// 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);
    }
}


Output:

Year 2017 is valid for monthday: --02-29 = false

References: https://docs.oracle.com/javase/10/docs/api/java/time/MonthDay.html#isValidYear(int)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
122 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12065 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7221 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS