YearMonth lengthOfMonth method() in Java
The lengthOfMonth() method of YearMonth class in Java is used to return the length of month of this year-month object’s value in number of days. The value of length of month is in the range of 1 to 31. This method also take cares of leap years.
Syntax:
public int lengthOfMonth()
Parameter: This method does not accepts any parameter.
Return Value: It returns an integer value denoting the length of month in number of days.
Below programs illustrate the lengthOfMonth() method of YearMonth in Java:
Program 1:
// Program to illustrate the lengthOfMonth() 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); // Print the length of month in Number // of days, 29 in this case as 2016 is // a Leap Year System.out.println(yearMonth.lengthOfMonth()); } } |
29
Program 2:
// Program to illustrate the lengthOfMonth() 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); // Print the length of month in Number // of days, 28 in this case as 1990 is // not a Leap Year System.out.println(yearMonth.lengthOfMonth()); } } |
28
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/YearMonth.html#lengthOfMonth–
Recommended Posts:
- ChronoLocalDate lengthOfMonth() method in Java with Examples
- LocalDate lengthOfMonth() method in Java with Examples
- YearMonth getYear() method in Java
- YearMonth hashCode() method in Java
- YearMonth with() Method in Java with Examples
- YearMonth atDay() method in Java
- YearMonth getMonthValue() method in Java
- YearMonth compareTo() method in Java
- YearMonth equals() method in Java
- YearMonth atEndOfMonth() method in Java
- YearMonth format() method in Java
- YearMonth until() Method in Java with Examples
- YearMonth getMonth() method in Java
- YearMonth lengthOfYear() method in Java with Examples
- YearMonth withMonth() method in Java with Examples
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.



