Java Month Enum Methods with Examples

This article is the series of Java Date Time API TutorialIn this article, we will discuss important methods or APIs of the Java Month enum from the java.time package.

The Month is an enum representing the 12 months of the year - January, February, March, April, May, June, July, August, September, October, November, and December.
In addition to the textual enum name, each month-of-year has an int value. The int value follows normal usage and the ISO-8601 standard, from 1 (January) to 12 (December). It is recommended that applications use the enum rather than the int value to ensure code clarity.
Important: Do not use ordinal() to obtain the numeric representation of Month. Use getValue() instead.

Java Month Enum Class Diagram

The below class diagram shows a list of methods that the Month enum provides.

Java Month Enum Methods/APIs with Examples

Java Month Enum Methods/APIs

Let's list all important Month enum methods and the next section will show it's usage with an example.
  • int firstDayOfYear(boolean leapYear) - Gets the day-of-year corresponding to the first day of this month.
  • Month firstMonthOfQuarter() - Gets the month corresponding to the first month of this quarter.
  • static Month from(TemporalAccessor temporal) - Obtains an instance of Month from a temporal object.
  • int get(TemporalField field) - Gets the value of the specified field from this month-of-year as an int.
  • String getDisplayName(TextStyle style, Locale locale) - Gets the textual representation, such as 'Jan' or 'December'.
  • long getLong(TemporalField field) - Gets the value of the specified field from this month-of-year as a long.
  • int getValue() - Gets the month-of-year int value.
  • boolean isSupported(TemporalField field) - Checks if the specified field is supported.
  • int length(boolean leapYear) - Gets the length of this month in days.
  • int maxLength() - Gets the maximum length of this month in days.
  • int minLength() - Gets the minimum length of this month in days.
  • Month minus(long months) - Returns the month-of-year that is the specified number of months before this one.
  • static Month of(int month) - Obtains an instance of Month from an int value.
  • Month plus(long months) - Returns the month-of-year that is the specified number of quarters after this one.
  • ValueRange range(TemporalField field) - Gets the range of valid values for the specified field.
  • static Month valueOf(String name) - Returns the enum constant of this type with the specified name.
  • static Month[] values() - Returns an array containing the constants of this enum type, in the order they are declared.

Month Enum Methods/APIs Examples

Let's demonstrates the usage of a few important and commonly used Month Enum methods with examples. In below MonthMethodsExample class, each method name describes the Month enum method and its usage. Refer https://docs.oracle.com/javase/8/docs/api/java/time/Month.html Java doc for more details.
package net.javaguides.date;

import java.time.Month;
import java.time.ZonedDateTime;
import java.time.format.TextStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
import java.util.Locale;

public class MonthMethodsExample {

    public static void main(String[] args) {
        MonthMethodsExample example = new MonthMethodsExample();
        example.valuesMethod();

        example.valueOfMethodExample();

        example.rangeMethodExample();

        example.queryMethodExample();

        example.plusMethodExample();

        example.ofMethodExample();

        example.minusMethodExample();

        example.minLengthMethodExample();

        example.maxLengthMethodExample();

        example.getValueMethodExample();

        example.getLongMethodExample();

        example.getDisplayNameMethodExample();

        example.getMethodExample();

        example.fromMethodExample();

        example.firstMonthOfQuarterMethodExample();

        example.firstDayOfYearMethodExample();

    }

    public void valuesMethod() {
        for (Month month: Month.values()) {
            System.out.println(month);
        }
    }

    public void valueOfMethodExample() {
        Month month = Month.valueOf("MARCH");
        System.out.println(month);
    }

    public void rangeMethodExample() {
        Month day = Month.of(2);
        System.out.println("Range : " + day.range(ChronoField.MONTH_OF_YEAR));
    }

    public void queryMethodExample() {
        Month month = Month.of(2);
        System.out.printf("Month precision is %s%n", month.query(TemporalQueries.precision()));
    }

    public void plusMethodExample() {
        Month month = Month.of(5);
        System.out.println(month);
        System.out.println(month.plus(1));
    }

    public void ofMethodExample() {
        Month month = Month.of(3);
        System.out.println(month);
    }

    public void minusMethodExample() {
        Month month = Month.of(5);
        System.out.println(month);
        System.out.println(month.minus(1));
    }

    public void minLengthMethodExample() {
        Month month = Month.FEBRUARY;
        System.out.println(month.minLength());
    }

    public void maxLengthMethodExample() {
        Month month = Month.FEBRUARY;
        System.out.println(month.maxLength());
    }

    public void getValueMethodExample() {
        Month month = Month.FEBRUARY;
        System.out.println(month.getValue());
    }

    public void getLongMethodExample() {
        Month day = Month.FEBRUARY;
        System.out.println(day.getLong(ChronoField.MONTH_OF_YEAR));
    }

    public void getDisplayNameMethodExample() {
        Month day = Month.of(3);
        System.out.println(day.getDisplayName(TextStyle.SHORT, Locale.ENGLISH));
    }

    public void getMethodExample() {
        Month day = Month.FEBRUARY;
        System.out.println(day.get(ChronoField.MONTH_OF_YEAR));
    }

    public void fromMethodExample() {
        Month day = Month.from(ZonedDateTime.now());
        System.out.println(day);
    }

    public void firstMonthOfQuarterMethodExample() {
        Month month = Month.of(3);
        System.out.println(month.firstMonthOfQuarter());
    }

    public void firstDayOfYearMethodExample() {
        Month month = Month.of(3);
        System.out.println(month.firstDayOfYear(false));
    }
}
Output:
JANUARY
FEBRUARY
MARCH
APRIL
MAY
JUNE
JULY
AUGUST
SEPTEMBER
OCTOBER
NOVEMBER
DECEMBER
MARCH
Range : 1 - 12
Month precision is Months
MAY
JUNE
MARCH
MAY
APRIL
28
29
2
2
Mar
2
DECEMBER
JANUARY
60

Related Java 8 Date & Time API Guide

References


Comments