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)); }} |
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)); }} |
false
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#isValidMonthDay-java.time.MonthDay-



