Month length() method in Java

The length() method is a built-in method of the Month ENUM which is used to get the number of days in this month instance. The number of days in a month can be 28, 30 or 31. Number of days in February in a leap year is 29.

This method accepts a boolean flag variable which indicates whether this Year is a leap year or not.

Syntax:



public int length(boolean leapYear)

Parameters: This method accepts a single parameter leapYear, which indicates whether this year is a leapYear or not.

Return Value: This method returns the length of this month in number of days present in it.

Below programs illustrate the above method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.time.*;
import java.time.Month;
import java.time.temporal.ChronoField;
  
class monthEnum {
    public static void main(String[] args)
    {
        // Create a month instance
        Month month = Month.MAY;
  
        // Print the length of this Month
        System.out.println(month.length(false));
    }
}

chevron_right


Output:

31

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.time.*;
import java.time.Month;
import java.time.temporal.ChronoField;
  
class monthEnum {
    public static void main(String[] args)
    {
        // Create a month instance
        Month month = Month.FEBRUARY;
  
        // Print the length of this Month
        System.out.println(month.length(true));
    }
}

chevron_right


Output:

29

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Month.html#length-boolean-



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.