YearMonth isValidDay() method in Java with Examples

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

Syntax:

public boolean isValidDay(int monthDay)

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

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

Below programs illustrate the YearMonth.isValidDay() method in Java:

Program 1:




// Program to illustrate the isValidDay() method
Ā Ā 
import java.util.*;
import java.time.*;
Ā Ā 
public class GfG {
Ā Ā Ā Ā public static void main(String[] args)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // Create YearMonth object
Ā Ā Ā Ā Ā Ā Ā Ā YearMonth yearMonth = YearMonth.of(2016, 2);
Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Check if the day passed is valid
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(yearMonth.isValidDay(24));
Ā Ā Ā Ā }
}


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 isValidDay() method
Ā Ā 
import java.util.*;
import java.time.*;
Ā Ā 
public class GfG {
Ā Ā Ā Ā public static void main(String[] args)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // Create YearMonth object
Ā Ā Ā Ā Ā Ā Ā Ā YearMonth yearMonth = YearMonth.of(1990, 2);
Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Check if the day passed is valid
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(yearMonth.isValidDay(29));
Ā Ā Ā Ā }
}


Output:

false

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

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button