Java : YearMonth with Examples
YearMonth (Java SE 21 & JDK 21) with Examples.
You will find code examples on most YearMonth methods.
Summary
final var yearMonth = YearMonth.of(2100, Month.AUGUST);
System.out.println(yearMonth); // 2100-08
System.out.println(yearMonth.getYear()); // 2100
System.out.println(yearMonth.getMonth()); // AUGUST
System.out.println(yearMonth.getMonthValue()); // 8
Methods
Temporal adjustInto (Temporal temporal)
final var yearMonth = YearMonth.of(1999, Month.DECEMBER);
System.out.println(yearMonth); // 1999-12
final var date = LocalDate.of(2100, 2, 3);
System.out.println(date); // 2100-02-03
final var ret = yearMonth.adjustInto(date);
System.out.println(ret); // 1999-12-03
LocalDate atDay (int dayOfMonth)
final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01
System.out.println(yearMonth.atDay(2)); // 2100-01-02
System.out.println(yearMonth.atDay(3)); // 2100-01-03
System.out.println(yearMonth.atDay(31)); // 2100-01-31
try {
final var ret = yearMonth.atDay(32);
} catch (DateTimeException e) {
System.out.println("DateTimeException! : " + e.getMessage());
}
// Result
// ↓
//DateTimeException! : Invalid value for DayOfMonth (valid values 1 - 28/31): 32
LocalDate atEndOfMonth ()
final var yearMonth = YearMonth.of(2103, Month.JANUARY);
System.out.println(yearMonth); // 2103-01
System.out.println(yearMonth.atEndOfMonth()); // 2103-01-31
System.out.println(yearMonth.lengthOfMonth()); // 31
final var yearMonth = YearMonth.of(2103, Month.FEBRUARY);
System.out.println(yearMonth); // 2103-02
System.out.println(yearMonth.isLeapYear()); // false
System.out.println(yearMonth.atEndOfMonth()); // 2103-02-28
System.out.println(yearMonth.lengthOfMonth()); // 28
final var yearMonth = YearMonth.of(2104, Month.FEBRUARY);
System.out.println(yearMonth); // 2104-02
System.out.println(yearMonth.isLeapYear()); // true
System.out.println(yearMonth.atEndOfMonth()); // 2104-02-29
System.out.println(yearMonth.lengthOfMonth()); // 29
int compareTo (YearMonth other)
final var yearMonth1 = YearMonth.of(2100, Month.JANUARY);
final var yearMonth2 = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth1); // 2100-01
System.out.println(yearMonth2); // 2100-01
System.out.println(yearMonth1.compareTo(yearMonth2)); // 0
System.out.println(yearMonth2.compareTo(yearMonth1)); // 0
final var yearMonth1 = YearMonth.of(1999, Month.MAY);
final var yearMonth2 = YearMonth.of(2100, Month.MAY);
System.out.println(yearMonth1); // 1999-05
System.out.println(yearMonth2); // 2100-05
System.out.println(yearMonth1.compareTo(yearMonth2)); // -101
System.out.println(yearMonth2.compareTo(yearMonth1)); // 101
final var yearMonth1 = YearMonth.of(2100, Month.DECEMBER);
final var yearMonth2 = YearMonth.of(2100, Month.APRIL);
System.out.println(yearMonth1); // 2100-12
System.out.println(yearMonth2); // 2100-04
System.out.println(yearMonth1.compareTo(yearMonth2)); // 8
System.out.println(yearMonth2.compareTo(yearMonth1)); // -8
boolean equals (Object obj)
final var yearMonth1 = YearMonth.of(2100, Month.JANUARY);
final var yearMonth2 = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth1); // 2100-01
System.out.println(yearMonth2); // 2100-01
System.out.println(yearMonth1.equals(yearMonth2)); // true
System.out.println(yearMonth2.equals(yearMonth1)); // true
final var yearMonth1 = YearMonth.of(1999, Month.MAY);
final var yearMonth2 = YearMonth.of(2100, Month.MAY);
System.out.println(yearMonth1); // 1999-05
System.out.println(yearMonth2); // 2100-05
System.out.println(yearMonth1.equals(yearMonth2)); // false
System.out.println(yearMonth2.equals(yearMonth1)); // false
final var yearMonth1 = YearMonth.of(2100, Month.DECEMBER);
final var yearMonth2 = YearMonth.of(2100, Month.APRIL);
System.out.println(yearMonth1); // 2100-12
System.out.println(yearMonth2); // 2100-04
System.out.println(yearMonth1.equals(yearMonth2)); // false
System.out.println(yearMonth2.equals(yearMonth1)); // false
String format (DateTimeFormatter formatter)
final var yearMonth = YearMonth.of(2099, Month.JANUARY);
System.out.println(yearMonth); // 2099-01
final var ret1 = yearMonth.format(DateTimeFormatter.ofPattern("yyyy-MM"));
System.out.println(ret1); // 2099-01
final var ret2 = yearMonth.format(DateTimeFormatter.ofPattern("yy-M"));
System.out.println(ret2); // 99-1
static YearMonth from (TemporalAccessor temporal)
final var date = LocalDate.of(2100, 2, 3);
System.out.println(date); // 2100-02-03
final var ret = YearMonth.from(date);
System.out.println(ret); // 2100-02
final var dateTime = LocalDateTime.of(1999, 1, 1, 12, 30);
System.out.println(dateTime); // 1999-01-01T12:30
final var ret = YearMonth.from(dateTime);
System.out.println(ret); // 1999-01
int get (TemporalField field)
final var yearMonth = YearMonth.of(2100, Month.AUGUST);
System.out.println(yearMonth); // 2100-08
System.out.println(yearMonth.get(ChronoField.YEAR)); // 2100
System.out.println(yearMonth.get(ChronoField.MONTH_OF_YEAR)); // 8
System.out.println(yearMonth.get(ChronoField.ERA)); // 1
final var yearMonth = YearMonth.of(-100, Month.DECEMBER);
System.out.println(yearMonth); // -0100-12
System.out.println(yearMonth.get(ChronoField.YEAR)); // -100
System.out.println(yearMonth.get(ChronoField.MONTH_OF_YEAR)); // 12
System.out.println(yearMonth.get(ChronoField.ERA)); // 0
long getLong (TemporalField field)
final var yearMonth = YearMonth.of(2100, Month.AUGUST);
System.out.println(yearMonth); // 2100-08
System.out.println(yearMonth.getLong(ChronoField.YEAR)); // 2100
System.out.println(yearMonth.getLong(ChronoField.MONTH_OF_YEAR)); // 8
System.out.println(yearMonth.getLong(ChronoField.ERA)); // 1
final var yearMonth = YearMonth.of(-100, Month.DECEMBER);
System.out.println(yearMonth); // -0100-12
System.out.println(yearMonth.getLong(ChronoField.YEAR)); // -100
System.out.println(yearMonth.getLong(ChronoField.MONTH_OF_YEAR)); // 12
System.out.println(yearMonth.getLong(ChronoField.ERA)); // 0
Month getMonth ()
final var yearMonth = YearMonth.of(2100, Month.AUGUST);
System.out.println(yearMonth); // 2100-08
System.out.println(yearMonth.getYear()); // 2100
System.out.println(yearMonth.getMonth()); // AUGUST
System.out.println(yearMonth.getMonthValue()); // 8
int getMonthValue ()
final var yearMonth = YearMonth.of(2100, Month.AUGUST);
System.out.println(yearMonth); // 2100-08
System.out.println(yearMonth.getYear()); // 2100
System.out.println(yearMonth.getMonth()); // AUGUST
System.out.println(yearMonth.getMonthValue()); // 8
int getYear ()
final var yearMonth = YearMonth.of(2100, Month.AUGUST);
System.out.println(yearMonth); // 2100-08
System.out.println(yearMonth.getYear()); // 2100
System.out.println(yearMonth.getMonth()); // AUGUST
System.out.println(yearMonth.getMonthValue()); // 8
int hashCode ()
final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01
System.out.println(yearMonth.hashCode()); // 134219828
final var yearMonth = YearMonth.of(-100, Month.AUGUST);
System.out.println(yearMonth); // -0100-08
System.out.println(yearMonth.hashCode()); // -1073741924
boolean isAfter (YearMonth other)
final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01
final var yearMonth2 = YearMonth.of(2099, Month.DECEMBER);
final var yearMonth3 = YearMonth.of(2100, Month.JANUARY);
final var yearMonth4 = YearMonth.of(2100, Month.FEBRUARY);
System.out.println(yearMonth2); // 2099-12
System.out.println(yearMonth3); // 2100-01
System.out.println(yearMonth4); // 2100-02
System.out.println(yearMonth.isAfter(yearMonth2)); // true
System.out.println(yearMonth.isAfter(yearMonth3)); // false
System.out.println(yearMonth.isAfter(yearMonth4)); // false
boolean isBefore (YearMonth other)
final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01
final var yearMonth2 = YearMonth.of(2099, Month.DECEMBER);
final var yearMonth3 = YearMonth.of(2100, Month.JANUARY);
final var yearMonth4 = YearMonth.of(2100, Month.FEBRUARY);
System.out.println(yearMonth2); // 2099-12
System.out.println(yearMonth3); // 2100-01
System.out.println(yearMonth4); // 2100-02
System.out.println(yearMonth.isBefore(yearMonth2)); // false
System.out.println(yearMonth.isBefore(yearMonth3)); // false
System.out.println(yearMonth.isBefore(yearMonth4)); // true
boolean isLeapYear ()
final var yearMonth = YearMonth.of(2103, Month.FEBRUARY);
System.out.println(yearMonth); // 2103-02
System.out.println(yearMonth.isLeapYear()); // false
System.out.println(yearMonth.atEndOfMonth()); // 2103-02-28
System.out.println(yearMonth.lengthOfYear()); // 365
final var yearMonth = YearMonth.of(2104, Month.FEBRUARY);
System.out.println(yearMonth); // 2104-02
System.out.println(yearMonth.isLeapYear()); // true
System.out.println(yearMonth.atEndOfMonth()); // 2104-02-29
System.out.println(yearMonth.lengthOfYear()); // 366
boolean isSupported (TemporalField field)
final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01
System.out.println(yearMonth.isSupported(ChronoField.YEAR)); // true
System.out.println(yearMonth.isSupported(ChronoField.YEAR_OF_ERA)); // true
System.out.println(yearMonth.isSupported(ChronoField.ERA)); // true
System.out.println(yearMonth.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(yearMonth.isSupported(ChronoField.PROLEPTIC_MONTH)); // true
System.out.println(yearMonth.isSupported(ChronoField.DAY_OF_MONTH)); // false
boolean isSupported (TemporalUnit unit)
final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01
System.out.println(yearMonth.isSupported(ChronoUnit.YEARS)); // true
System.out.println(yearMonth.isSupported(ChronoUnit.MONTHS)); // true
System.out.println(yearMonth.isSupported(ChronoUnit.DECADES)); // true
System.out.println(yearMonth.isSupported(ChronoUnit.CENTURIES)); // true
System.out.println(yearMonth.isSupported(ChronoUnit.MILLENNIA)); // true
System.out.println(yearMonth.isSupported(ChronoUnit.ERAS)); // true
System.out.println(yearMonth.isSupported(ChronoUnit.DAYS)); // false
boolean isValidDay (int dayOfMonth)
final var yearMonth = YearMonth.of(2100, Month.APRIL);
System.out.println(yearMonth); // 2100-04
System.out.println(yearMonth.atEndOfMonth()); // 2100-04-30
System.out.println(yearMonth.isValidDay(1)); // true
System.out.println(yearMonth.isValidDay(15)); // true
System.out.println(yearMonth.isValidDay(30)); // true
System.out.println(yearMonth.isValidDay(31)); // false
int lengthOfMonth ()
final var yearMonth = YearMonth.of(2103, Month.JANUARY);
System.out.println(yearMonth); // 2103-01
System.out.println(yearMonth.atEndOfMonth()); // 2103-01-31
System.out.println(yearMonth.lengthOfMonth()); // 31
final var yearMonth = YearMonth.of(2103, Month.FEBRUARY);
System.out.println(yearMonth); // 2103-02
System.out.println(yearMonth.isLeapYear()); // false
System.out.println(yearMonth.atEndOfMonth()); // 2103-02-28
System.out.println(yearMonth.lengthOfMonth()); // 28
final var yearMonth = YearMonth.of(2104, Month.FEBRUARY);
System.out.println(yearMonth); // 2104-02
System.out.println(yearMonth.isLeapYear()); // true
System.out.println(yearMonth.atEndOfMonth()); // 2104-02-29
System.out.println(yearMonth.lengthOfMonth()); // 29
int lengthOfYear ()
final var yearMonth = YearMonth.of(2103, Month.FEBRUARY);
System.out.println(yearMonth); // 2103-02
System.out.println(yearMonth.isLeapYear()); // false
System.out.println(yearMonth.atEndOfMonth()); // 2103-02-28
System.out.println(yearMonth.lengthOfYear()); // 365
final var yearMonth = YearMonth.of(2104, Month.FEBRUARY);
System.out.println(yearMonth); // 2104-02
System.out.println(yearMonth.isLeapYear()); // true
System.out.println(yearMonth.atEndOfMonth()); // 2104-02-29
System.out.println(yearMonth.lengthOfYear()); // 366
YearMonth minus (long amountToSubtract, TemporalUnit unit)
final var yearMonth = YearMonth.of(2100, Month.FEBRUARY);
System.out.println(yearMonth); // 2100-02
final var ret1 = yearMonth.minus(10, ChronoUnit.YEARS);
System.out.println(ret1); // 2090-02
final var ret2 = yearMonth.minus(20, ChronoUnit.YEARS);
System.out.println(ret2); // 2080-02
final var ret3 = yearMonth.minus(1, ChronoUnit.MONTHS);
System.out.println(ret3); // 2100-01
final var ret4 = yearMonth.minus(2, ChronoUnit.MONTHS);
System.out.println(ret4); // 2099-12
YearMonth minus (TemporalAmount amountToSubtract)
final var yearMonth = YearMonth.of(2100, Month.FEBRUARY);
System.out.println(yearMonth); // 2100-02
final var ret1 = yearMonth.minus(Period.ofYears(10));
System.out.println(ret1); // 2090-02
final var ret2 = yearMonth.minus(Period.ofYears(20));
System.out.println(ret2); // 2080-02
final var ret3 = yearMonth.minus(Period.ofMonths(1));
System.out.println(ret3); // 2100-01
final var ret4 = yearMonth.minus(Period.ofMonths(2));
System.out.println(ret4); // 2099-12
YearMonth minusMonths (long monthsToSubtract)
final var yearMonth = YearMonth.of(2100, Month.FEBRUARY);
System.out.println(yearMonth); // 2100-02
final var ret1 = yearMonth.minusMonths(1);
System.out.println(ret1); // 2100-01
final var ret2 = yearMonth.minusMonths(2);
System.out.println(ret2); // 2099-12
YearMonth minusYears (long yearsToSubtract)
final var yearMonth = YearMonth.of(2100, Month.FEBRUARY);
System.out.println(yearMonth); // 2100-02
final var ret1 = yearMonth.minusYears(10);
System.out.println(ret1); // 2090-02
final var ret2 = yearMonth.minusYears(20);
System.out.println(ret2); // 2080-02
static YearMonth now ()
final var now = YearMonth.now();
System.out.println(now); // 2024-07
static YearMonth now (Clock clock)
// A clock advanced 1 year.
final var clock = Clock.offset(Clock.systemDefaultZone(), Duration.ofDays(365));
System.out.println(YearMonth.now()); // 2024-07
System.out.println(YearMonth.now(clock)); // 2025-07
static YearMonth now (ZoneId zone)
final var zone = ZoneOffset.UTC;
System.out.println(zone); // Z
final var now = YearMonth.now(zone);
System.out.println(now); // 2024-07
static YearMonth of (int year, int month)
final var yearMonth = YearMonth.of(2100, 1);
System.out.println(yearMonth.getYear()); // 2100
System.out.println(yearMonth.getMonth()); // JANUARY
System.out.println(yearMonth.getMonthValue()); // 1
final var yearMonth = YearMonth.of(1999, 11);
System.out.println(yearMonth.getYear()); // 1999
System.out.println(yearMonth.getMonth()); // NOVEMBER
System.out.println(yearMonth.getMonthValue()); // 11
static YearMonth of (int year, Month month)
final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth.getYear()); // 2100
System.out.println(yearMonth.getMonth()); // JANUARY
System.out.println(yearMonth.getMonthValue()); // 1
final var yearMonth = YearMonth.of(1999, Month.NOVEMBER);
System.out.println(yearMonth.getYear()); // 1999
System.out.println(yearMonth.getMonth()); // NOVEMBER
System.out.println(yearMonth.getMonthValue()); // 11
static YearMonth parse (CharSequence text)
final var yearMonth1 = YearMonth.parse("2100-01");
System.out.println(yearMonth1); // 2100-01
final var yearMonth2 = YearMonth.parse("1999-12");
System.out.println(yearMonth2); // 1999-12
static YearMonth parse (CharSequence text, DateTimeFormatter formatter)
final var yearMonth1 = YearMonth.parse(
"2100-01", DateTimeFormatter.ofPattern("yyyy-MM"));
System.out.println(yearMonth1); // 2100-01
final var yearMonth2 = YearMonth.parse(
"99-5", DateTimeFormatter.ofPattern("yy-M"));
System.out.println(yearMonth2); // 2099-05
YearMonth plus (long amountToAdd, TemporalUnit unit)
final var yearMonth = YearMonth.of(2100, Month.NOVEMBER);
System.out.println(yearMonth); // 2100-11
final var ret1 = yearMonth.plus(50, ChronoUnit.YEARS);
System.out.println(ret1); // 2150-11
final var ret2 = yearMonth.plus(60, ChronoUnit.YEARS);
System.out.println(ret2); // 2160-11
final var ret3 = yearMonth.plus(1, ChronoUnit.MONTHS);
System.out.println(ret3); // 2100-12
final var ret4 = yearMonth.plus(2, ChronoUnit.MONTHS);
System.out.println(ret4); // 2101-01
YearMonth plus (TemporalAmount amountToAdd)
final var yearMonth = YearMonth.of(2100, Month.NOVEMBER);
System.out.println(yearMonth); // 2100-11
final var ret1 = yearMonth.plus(Period.ofYears(50));
System.out.println(ret1); // 2150-11
final var ret2 = yearMonth.plus(Period.ofYears(60));
System.out.println(ret2); // 2160-11
final var ret3 = yearMonth.plus(Period.ofMonths(1));
System.out.println(ret3); // 2100-12
final var ret4 = yearMonth.plus(Period.ofMonths(2));
System.out.println(ret4); // 2101-01
YearMonth plusMonths (long monthsToAdd)
final var yearMonth = YearMonth.of(2100, Month.NOVEMBER);
System.out.println(yearMonth); // 2100-11
final var ret1 = yearMonth.plusMonths(1);
System.out.println(ret1); // 2100-12
final var ret2 = yearMonth.plusMonths(2);
System.out.println(ret2); // 2101-01
YearMonth plusYears (long yearsToAdd)
final var yearMonth = YearMonth.of(2100, Month.NOVEMBER);
System.out.println(yearMonth); // 2100-11
final var ret1 = yearMonth.plusYears(50);
System.out.println(ret1); // 2150-11
final var ret2 = yearMonth.plusYears(60);
System.out.println(ret2); // 2160-11
<R> R query (TemporalQuery<R> query)
final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01
final var ret = yearMonth.query(TemporalQueries.precision());
System.out.println(ret); // Months
ValueRange range (TemporalField field)
final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01
System.out.println(yearMonth.range(ChronoField.YEAR)); // -999999999 - 999999999
System.out.println(yearMonth.range(ChronoField.MONTH_OF_YEAR)); // 1 - 12
String toString ()
final var yearMonth = YearMonth.of(2100, Month.JANUARY);
final var str = yearMonth.toString();
System.out.println(str); // 2100-01
final var yearMonth = YearMonth.of(1999, Month.OCTOBER);
final var str = yearMonth.toString();
System.out.println(str); // 1999-10
long until (Temporal endExclusive, TemporalUnit unit)
final var start = YearMonth.of(2100, Month.JANUARY);
System.out.println(start); // 2100-01
final var end = YearMonth.of(2150, Month.JANUARY);
System.out.println(end); // 2150-01
final var ret = start.until(end, ChronoUnit.YEARS);
System.out.println(ret); // 50
final var start = YearMonth.of(2100, Month.JANUARY);
System.out.println(start); // 2100-01
final var end = YearMonth.of(2100, Month.AUGUST);
System.out.println(end); // 2100-08
final var ret = start.until(end, ChronoUnit.MONTHS);
System.out.println(ret); // 7
YearMonth with (TemporalAdjuster adjuster)
final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01
final var ret1 = yearMonth.with(Year.of(1999));
System.out.println(ret1); // 1999-01
final var ret2 = yearMonth.with(Month.AUGUST);
System.out.println(ret2); // 2100-08
YearMonth with (TemporalField field, long newValue)
final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01
final var ret1 = yearMonth.with(ChronoField.YEAR, 1999);
System.out.println(ret1); // 1999-01
final var ret2 = yearMonth.with(ChronoField.MONTH_OF_YEAR, 8);
System.out.println(ret2); // 2100-08
YearMonth withMonth (int month)
final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01
final var ret1 = yearMonth.withYear(1999);
System.out.println(ret1); // 1999-01
final var ret2 = yearMonth.withMonth(8);
System.out.println(ret2); // 2100-08
YearMonth withYear (int year)
final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01
final var ret1 = yearMonth.withYear(1999);
System.out.println(ret1); // 1999-01
final var ret2 = yearMonth.withMonth(8);
System.out.println(ret2); // 2100-08
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
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- ZonedDateTime
- ZoneId
- ZoneOffset