Java : Month with Examples
Month (Java SE 21 & JDK 21) with Examples.
You will find code examples on most Month methods.
Summary
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
System.out.println(Month.APRIL); // APRIL
System.out.println(Month.APRIL.getValue()); // 4
AUGUST
System.out.println(Month.AUGUST); // AUGUST
System.out.println(Month.AUGUST.getValue()); // 8
DECEMBER
System.out.println(Month.DECEMBER); // DECEMBER
System.out.println(Month.DECEMBER.getValue()); // 12
FEBRUARY
System.out.println(Month.FEBRUARY); // FEBRUARY
System.out.println(Month.FEBRUARY.getValue()); // 2
JANUARY
System.out.println(Month.JANUARY); // JANUARY
System.out.println(Month.JANUARY.getValue()); // 1
JULY
System.out.println(Month.JULY); // JULY
System.out.println(Month.JULY.getValue()); // 7
JUNE
System.out.println(Month.JUNE); // JUNE
System.out.println(Month.JUNE.getValue()); // 6
MARCH
System.out.println(Month.MARCH); // MARCH
System.out.println(Month.MARCH.getValue()); // 3
MAY
System.out.println(Month.MAY); // MAY
System.out.println(Month.MAY.getValue()); // 5
NOVEMBER
System.out.println(Month.NOVEMBER); // NOVEMBER
System.out.println(Month.NOVEMBER.getValue()); // 11
OCTOBER
System.out.println(Month.OCTOBER); // OCTOBER
System.out.println(Month.OCTOBER.getValue()); // 10
SEPTEMBER
System.out.println(Month.SEPTEMBER); // SEPTEMBER
System.out.println(Month.SEPTEMBER.getValue()); // 9
Methods
Temporal adjustInto (Temporal temporal)
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)
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 ()
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)
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)
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)
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)
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 ()
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)
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)
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 ()
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 ()
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)
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)
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)
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)
System.out.println(Month.JANUARY.query(TemporalQueries.precision())); // Months
ValueRange range (TemporalField field)
System.out.println(Month.JANUARY.range(ChronoField.MONTH_OF_YEAR)); // 1 - 12
static Month valueOf (String 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 ()
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
- Get the current time
- Basics of Date and Time
- Don't use the legacy Date and Calendar classes, use new APIs instead
- API Examples
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset