Open In App

Month length() method in Java

Last Updated : 22 Mar, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


Output:

31

Program 2:




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


Output:

29

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



Similar Reads

Year atMonth(Month month) method in Java
The atMonth(Month) method of Year class in Java combines the current year object with a month passed as parameter to it to create a YearMonth object. Syntax: public YearMonth atMonth(Month month) Parameter: This method accepts a single parameter month. It is the month-of-year to use. It takes a valid Month object and cannot be NULL. Return Value: I
2 min read
Month adjustInto() method in Java
The adjustInto() method of java.time.Month ENUM is an in-built function in Java which takes a Temporal object specifying a date and returns a new Temporal object of the same observable type as the input with the month replaced with this month-of-year. Method Declaration: public Temporal adjustInto(Temporal temporal) Syntax: Temporal newLocalDate =
2 min read
Month firstDayOfYear() method in Java
The firstDayOfYear() is a built-in method of the Month ENUM which is used to get the day of year corresponding to the first day of this month. Syntax: public int firstDayOfYear(boolean leapYear) Parameters: This method accepts a single parameter leapYear, which is a boolean flag variable indicating whether this Year is a leap year or not. Return Va
1 min read
Month firstMonthOfQuarter() method in Java
The firstMonthOfQuarter() is a built-in method of the Month ENUM which is used to get the first month corresponding to this quarter. The quarter is defined by dividing the year into 4 groups as: Group 1: JANUARY, FEBRUARY, MARCH Group 2: APRIL, MAY, JUNE Group 3: JULY, AUGUST, SEPTEMBER Group 4: OCTOBER, NOVEMBER, DECEMBER Syntax: public int firstM
1 min read
Month from() method in Java
The from() is a built-in method of the Month ENUM which is used to create a Month instance from a temporal object passed to it as a parameter. Syntax: static Month from( TemporalAccessor temporal ) Parameters: This method accepts a single parameter which is a temporal object and cannot be NULL. Return Value: This method returns a Month instance obt
1 min read
Month get() method in Java
The get() method is a built-in method of the Month ENUM which is used to get the corresponding integral value of the month-of-year values specified by this Month instance. Syntax: public int get(TemporalField field) Parameters: This method accepts a single parameter which is a temporal object and cannot be NULL. Return Value: This method returns an
1 min read
Month getDisplayName() method in Java
The getDisplayName() method is a built-in method of the Month ENUM which is used to get the textual representation of the month-of-year specified by this Month instance. Syntax: public String getDisplayName(TextStyle style, Locale locale) Parameters: This method accepts two parameters as described below: style: This parameter specifies the style or
1 min read
Month getLong() method in Java
The getLong() method is a built-in method of the Month ENUM which is used to get the value of the specified temporal field from this month instance as a long. Syntax: public long getLong(TemporalField field) Parameters: This method accepts a single parameter field whose long representation will be returned from this month instance. Return Value: Th
1 min read
Month getValue() method in Java
The getValue() method is a built-in method of the Month ENUM which is used to get the value of the month-of-year from this Month instance as an integer. The value returned by this method is in the range of 1-12, representing months from January to December. Syntax: public int getValue() Parameters: This method does not accepts any parameters. Retur
1 min read
Month isSupported() method in Java
The isSupported() method is a built-in method of the Month ENUM which is used to check if the specified field is supported or not. This method accepts a fields as a parameter and returns true or false based on whether the field is supported or not. Syntax: public boolean isSupported(TemporalField field) Parameters: This method accepts a single para
2 min read
Practice Tags :