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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

false

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.