Java : YearMonth with Examples

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


Summary

A year-month in the ISO-8601 calendar system, such as 2007-12.

Class diagram

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)

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

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)

Combines this year-month with a day-of-month to create a LocalDate.

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 ()

Returns a LocalDate at the end of the month.

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)

Compares this year-month to another year-month.

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)

Checks if this year-month is equal to another year-month.

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)

Formats this year-month using the specified 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)

Obtains an instance of YearMonth from a temporal object.

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)

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

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)

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

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 ()

Gets the month-of-year field using the Month enum.

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 ()

Gets the month-of-year field from 1 to 12.

Please see getMonth().

int getYear ()

Gets the year field.

Please see getMonth().

int hashCode ()

A hash code for this year-month.

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)

Checks if this year-month is after the specified year-month.

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)

Checks if this year-month is before the specified year-month.

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 ()

Checks if the year is a leap year, according to the ISO proleptic calendar system rules.

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)

Checks if the specified field is supported.

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)

Checks if the specified unit is supported.

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)

Checks if the day-of-month is valid for this year-month.

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 ()

Returns the length of the month, taking account of the year.

Please see atEndOfMonth().

int lengthOfYear ()

Returns the length of the year.

Please see isLeapYear().

YearMonth minus (long amountToSubtract, TemporalUnit unit)

Returns a copy of this year-month with the specified amount subtracted.

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)

Returns a copy of this year-month with the specified amount subtracted.

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)

Returns a copy of this YearMonth with the specified number of months subtracted.

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)

Returns a copy of this YearMonth with the specified number of years subtracted.

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 ()

Obtains the current year-month from the system clock in the default time-zone.

final var now = YearMonth.now();
System.out.println(now); // 2023-10

static YearMonth now (Clock clock)

Obtains the current year-month from the specified clock.

final var clock = Clock.offset(Clock.systemDefaultZone(), Duration.ofDays(365));

System.out.println(YearMonth.now()); // 2023-10
System.out.println(YearMonth.now(clock)); // 2024-10

static YearMonth now (ZoneId zone)

Obtains the current year-month from the system clock in the specified time-zone.

final var zone = ZoneOffset.UTC;
System.out.println(zone); // Z

final var now = YearMonth.now(zone);
System.out.println(now); // 2023-10

static YearMonth of (int year, int month)

Obtains an instance of YearMonth from a year and 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)

Obtains an instance of YearMonth from a year and 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)

Obtains an instance of YearMonth from a text string such as 2007-12.

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)

Obtains an instance of YearMonth from a text string using a specific 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)

Returns a copy of this year-month with the specified amount added.

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)

Returns a copy of this year-month with the specified amount added.

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)

Returns a copy of this YearMonth with the specified number of months added.

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)

Returns a copy of this YearMonth with the specified number of years added.

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)

Queries this year-month using the specified 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)

Gets the range of valid values for the specified 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 ()

Outputs this year-month as a String, such as 2007-12.

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)

Calculates the amount of time until another year-month in terms of the specified 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)

Returns an adjusted copy of this year-month.

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)

Returns a copy of this year-month with the specified field set to a new value.

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)

Returns a copy of this YearMonth with the month-of-year altered.

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)

Returns a copy of this YearMonth with the year altered.

Please see withMonth(int month).


Related posts

To top of page