Saturday, December 13, 2025
HomeLanguagesJavaYear isValidMonthDay() method in Java

Year isValidMonthDay() method in Java

The isValidMonthDay() method of Year class in Java is used to check if this Year object and a month-day represented by a MonthDay provided as a parameter to the method together can form a valid date or not.

Syntax:

public boolean isValidMonthDay(MonthDay monthDay)

Parameter: This method accepts a single parameter monthDay which represents a month-day which is need to be examined with this Year object.

Return Value: It returns a boolean True value if this Year object and the given month-day represented by a MonthDay together can form a valid date otherwise it returns False.

Below programs illustrate the isValidMonthDay() method of Year in Java:
Program 1:




// Program to illustrate the isValidMonthDay() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
        // Create a Year object
        Year thisYear = Year.of(2016);
  
        // Creates a MonthDay object
        MonthDay monthDay = MonthDay.of(9, 15);
  
        // Check if this year object and given
        // MonthDay forms a valid date
        System.out.println(thisYear.isValidMonthDay(monthDay));
    }
}


Output:

true

Program 2: In the below program, Year is mentioned as 1990 which is not a leap year but month-day represents a leap year. So, they together can not form a valid date so the method will return false.




// Program to illustrate the isValidMonthDay() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
        // Create a Year object
        Year thisYear = Year.of(1990);
  
        // Creates a MonthDay object
        MonthDay monthDay = MonthDay.of(2, 29);
  
        // Check if this year object and given
        // MonthDay forms a valid date
        System.out.println(thisYear.isValidMonthDay(monthDay));
    }
}


Output:

false

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#isValidMonthDay-java.time.MonthDay-

RELATED ARTICLES

Most Popular

Dominic
32446 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6814 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12030 POSTS0 COMMENTS
Shaida Kate Naidoo
6949 POSTS0 COMMENTS
Ted Musemwa
7200 POSTS0 COMMENTS
Thapelo Manthata
6898 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS