Java : Month with Examples

Month (Java SE 21 & JDK 21) with Examples.
You will find code examples on most Month methods.


Summary

A month-of-year, such as 'July'.

Class diagram

final var month = Month.JANUARY;
System.out.println(month); // JANUARY

System.out.println(month.getDisplayName(TextStyle.FULL, Locale.US)); // January
System.out.println(month.getDisplayName(TextStyle.SHORT, Locale.US)); // Jan
final var date = LocalDate.of(2100, 7, 1);
System.out.println(date); // 2100-07-01

final var month = date.getMonth();
System.out.println(month); // JULY

Enum Constants

APRIL

The singleton instance for the month of April with 30 days.

System.out.println(Month.APRIL); // APRIL
System.out.println(Month.APRIL.getValue()); // 4

AUGUST

The singleton instance for the month of August with 31 days.

System.out.println(Month.AUGUST); // AUGUST
System.out.println(Month.AUGUST.getValue()); // 8

DECEMBER

The singleton instance for the month of December with 31 days.

System.out.println(Month.DECEMBER); // DECEMBER
System.out.println(Month.DECEMBER.getValue()); // 12

FEBRUARY

The singleton instance for the month of February with 28 days, or 29 in a leap year.

System.out.println(Month.FEBRUARY); // FEBRUARY
System.out.println(Month.FEBRUARY.getValue()); // 2

JANUARY

The singleton instance for the month of January with 31 days.

System.out.println(Month.JANUARY); // JANUARY
System.out.println(Month.JANUARY.getValue()); // 1

JULY

The singleton instance for the month of July with 31 days.

System.out.println(Month.JULY); // JULY
System.out.println(Month.JULY.getValue()); // 7

JUNE

The singleton instance for the month of June with 30 days.

System.out.println(Month.JUNE); // JUNE
System.out.println(Month.JUNE.getValue()); // 6

MARCH

The singleton instance for the month of March with 31 days.

System.out.println(Month.MARCH); // MARCH
System.out.println(Month.MARCH.getValue()); // 3

MAY

The singleton instance for the month of May with 31 days.

System.out.println(Month.MAY); // MAY
System.out.println(Month.MAY.getValue()); // 5

NOVEMBER

The singleton instance for the month of November with 30 days.

System.out.println(Month.NOVEMBER); // NOVEMBER
System.out.println(Month.NOVEMBER.getValue()); // 11

OCTOBER

The singleton instance for the month of October with 31 days.

System.out.println(Month.OCTOBER); // OCTOBER
System.out.println(Month.OCTOBER.getValue()); // 10

SEPTEMBER

The singleton instance for the month of September with 30 days.

System.out.println(Month.SEPTEMBER); // SEPTEMBER
System.out.println(Month.SEPTEMBER.getValue()); // 9

Methods

Temporal adjustInto (Temporal temporal)

Adjusts the specified temporal object to have this month-of-year.

final var temporal = LocalDate.of(2100, 3, 15);
System.out.println(temporal); // 2100-03-15

final var ret1 = Month.FEBRUARY.adjustInto(temporal);
System.out.println(ret1); // 2100-02-15

final var ret2 = Month.MARCH.adjustInto(temporal);
System.out.println(ret2); // 2100-03-15

final var ret3 = Month.APRIL.adjustInto(temporal);
System.out.println(ret3); // 2100-04-15

int firstDayOfYear (boolean leapYear)

Gets the day-of-year corresponding to the first day of this month.

System.out.println(Month.JANUARY.firstDayOfYear(false)); // 1
System.out.println(Month.FEBRUARY.firstDayOfYear(false)); // 32
System.out.println(Month.MARCH.firstDayOfYear(false)); // 60
System.out.println(Month.APRIL.firstDayOfYear(false)); // 91

System.out.println(Month.JANUARY.firstDayOfYear(true)); // 1
System.out.println(Month.FEBRUARY.firstDayOfYear(true)); // 32
System.out.println(Month.MARCH.firstDayOfYear(true)); // 61
System.out.println(Month.APRIL.firstDayOfYear(true)); // 92

Month firstMonthOfQuarter ()

Gets the month corresponding to the first month of this quarter.

System.out.println(Month.JANUARY.firstMonthOfQuarter()); // JANUARY
System.out.println(Month.FEBRUARY.firstMonthOfQuarter()); // JANUARY
System.out.println(Month.MARCH.firstMonthOfQuarter()); // JANUARY

System.out.println(Month.APRIL.firstMonthOfQuarter()); // APRIL
System.out.println(Month.MAY.firstMonthOfQuarter()); // APRIL
System.out.println(Month.JUNE.firstMonthOfQuarter()); // APRIL

System.out.println(Month.JULY.firstMonthOfQuarter()); // JULY
System.out.println(Month.AUGUST.firstMonthOfQuarter()); // JULY
System.out.println(Month.SEPTEMBER.firstMonthOfQuarter()); // JULY

System.out.println(Month.OCTOBER.firstMonthOfQuarter()); // OCTOBER
System.out.println(Month.NOVEMBER.firstMonthOfQuarter()); // OCTOBER
System.out.println(Month.DECEMBER.firstMonthOfQuarter()); // OCTOBER

static Month from (TemporalAccessor temporal)

Obtains an instance of Month from a temporal object.

final var temporal = LocalDate.of(2100, 1, 15);
System.out.println(temporal); // 2100-01-15

System.out.println(Month.from(temporal)); // JANUARY
final var temporal = LocalDate.of(1999, 2, 28);
System.out.println(temporal); // 1999-02-28

System.out.println(Month.from(temporal)); // FEBRUARY

int get (TemporalField field)

Gets the value of the specified field from this month-of-year as an int.

System.out.println(Month.JANUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.JANUARY.get(ChronoField.MONTH_OF_YEAR)); // 1

System.out.println(Month.FEBRUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.FEBRUARY.get(ChronoField.MONTH_OF_YEAR)); // 2
System.out.println(Month.FEBRUARY.isSupported(ChronoField.YEAR)); // false

try {
    final var ret = Month.FEBRUARY.get(ChronoField.YEAR);
} catch (UnsupportedTemporalTypeException e) {
    System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}

// Result
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: Year

String getDisplayName (TextStyle style, Locale locale)

Gets the textual representation, such as 'Jan' or 'December'.

final var locale = Locale.getDefault();
System.out.println(locale.toLanguageTag()); // en-US

System.out.println(Month.JANUARY.getDisplayName(TextStyle.FULL, locale)); // January
System.out.println(Month.JANUARY.getDisplayName(TextStyle.SHORT, locale)); // Jan
System.out.println(Month.JANUARY.getDisplayName(TextStyle.NARROW, locale)); // J

long getLong (TemporalField field)

Gets the value of the specified field from this month-of-year as a long.

System.out.println(Month.JANUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.JANUARY.getLong(ChronoField.MONTH_OF_YEAR)); // 1

System.out.println(Month.FEBRUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.FEBRUARY.getLong(ChronoField.MONTH_OF_YEAR)); // 2
System.out.println(Month.FEBRUARY.isSupported(ChronoField.YEAR)); // false

try {
    final var ret = Month.FEBRUARY.getLong(ChronoField.YEAR);
} catch (UnsupportedTemporalTypeException e) {
    System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}

// Result
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: Year

int getValue ()

Gets the month-of-year int value.

System.out.println(Month.JANUARY.getValue()); // 1
System.out.println(Month.FEBRUARY.getValue()); // 2
System.out.println(Month.MARCH.getValue()); // 3
System.out.println(Month.APRIL.getValue()); // 4
System.out.println(Month.MAY.getValue()); // 5
System.out.println(Month.JUNE.getValue()); // 6
System.out.println(Month.JULY.getValue()); // 7
System.out.println(Month.AUGUST.getValue()); // 8
System.out.println(Month.SEPTEMBER.getValue()); // 9
System.out.println(Month.OCTOBER.getValue()); // 10
System.out.println(Month.NOVEMBER.getValue()); // 11
System.out.println(Month.DECEMBER.getValue()); // 12

boolean isSupported (TemporalField field)

Checks if the specified field is supported.

System.out.println(Month.JANUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.JANUARY.get(ChronoField.MONTH_OF_YEAR)); // 1

System.out.println(Month.FEBRUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.FEBRUARY.get(ChronoField.MONTH_OF_YEAR)); // 2
System.out.println(Month.FEBRUARY.isSupported(ChronoField.YEAR)); // false

try {
    final var ret = Month.FEBRUARY.get(ChronoField.YEAR);
} catch (UnsupportedTemporalTypeException e) {
    System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}

// Result
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: Year

int length (boolean leapYear)

Gets the length of this month in days.

System.out.println(Month.JANUARY.length(false)); // 31
System.out.println(Month.FEBRUARY.length(false)); // 28
System.out.println(Month.MARCH.length(false)); // 31
System.out.println(Month.APRIL.length(false)); // 30

System.out.println(Month.JANUARY.length(true)); // 31
System.out.println(Month.FEBRUARY.length(true)); // 29
System.out.println(Month.MARCH.length(true)); // 31
System.out.println(Month.APRIL.length(true)); // 30

int maxLength ()

Gets the maximum length of this month in days.

System.out.println(Month.JANUARY.minLength()); // 31
System.out.println(Month.JANUARY.maxLength()); // 31

System.out.println(Month.FEBRUARY.minLength()); // 28
System.out.println(Month.FEBRUARY.maxLength()); // 29

System.out.println(Month.MARCH.minLength()); // 31
System.out.println(Month.MARCH.maxLength()); // 31

System.out.println(Month.APRIL.minLength()); // 30
System.out.println(Month.APRIL.maxLength()); // 30

int minLength ()

Gets the minimum length of this month in days.

System.out.println(Month.JANUARY.minLength()); // 31
System.out.println(Month.JANUARY.maxLength()); // 31

System.out.println(Month.FEBRUARY.minLength()); // 28
System.out.println(Month.FEBRUARY.maxLength()); // 29

System.out.println(Month.MARCH.minLength()); // 31
System.out.println(Month.MARCH.maxLength()); // 31

System.out.println(Month.APRIL.minLength()); // 30
System.out.println(Month.APRIL.maxLength()); // 30

Month minus (long months)

Returns the month-of-year that is the specified number of months before this one.

final var month = Month.FEBRUARY;
System.out.println(month.getValue()); // 2

final var ret1 = month.minus(1);
System.out.println(ret1.getValue()); // 1

final var ret2 = month.minus(2);
System.out.println(ret2.getValue()); // 12

final var ret3 = month.minus(11);
System.out.println(ret3.getValue()); // 3

final var ret4 = month.minus(12);
System.out.println(ret4.getValue()); // 2

static Month of (int month)

Obtains an instance of Month from an int value.

System.out.println(Month.of(1)); // JANUARY
System.out.println(Month.of(2)); // FEBRUARY
System.out.println(Month.of(3)); // MARCH
System.out.println(Month.of(4)); // APRIL
System.out.println(Month.of(5)); // MAY
System.out.println(Month.of(6)); // JUNE
System.out.println(Month.of(7)); // JULY
System.out.println(Month.of(8)); // AUGUST
System.out.println(Month.of(9)); // SEPTEMBER
System.out.println(Month.of(10)); // OCTOBER
System.out.println(Month.of(11)); // NOVEMBER
System.out.println(Month.of(12)); // DECEMBER

Month plus (long months)

Returns the month-of-year that is the specified number of months after this one.

final var month = Month.NOVEMBER;
System.out.println(month.getValue()); // 11

final var ret1 = month.plus(1);
System.out.println(ret1.getValue()); // 12

final var ret2 = month.plus(2);
System.out.println(ret2.getValue()); // 1

final var ret3 = month.plus(11);
System.out.println(ret3.getValue()); // 10

final var ret4 = month.plus(12);
System.out.println(ret4.getValue()); // 11

<R> R query (TemporalQuery<R> query)

Queries this month-of-year using the specified query.

System.out.println(Month.JANUARY.query(TemporalQueries.precision())); // Months

ValueRange range (TemporalField field)

Gets the range of valid values for the specified field.

System.out.println(Month.JANUARY.range(ChronoField.MONTH_OF_YEAR)); // 1 - 12

static Month valueOf (String name)

Returns the enum constant of this class with the specified name.

System.out.println(Month.valueOf("JANUARY")); // JANUARY
System.out.println(Month.valueOf("FEBRUARY")); // FEBRUARY
System.out.println(Month.valueOf("MARCH")); // MARCH

static Month[] values ()

Returns an array containing the constants of this enum class, in the order they are declared.

for (final var month : Month.values()) {
    System.out.println(month);
}

// Result
// ↓
//JANUARY
//FEBRUARY
//MARCH
//APRIL
//MAY
//JUNE
//JULY
//AUGUST
//SEPTEMBER
//OCTOBER
//NOVEMBER
//DECEMBER

Methods declared in Enum

clone, compareTo, describeConstable, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf

Please see the link below.


Related posts

To top of page