Java : MonthDay with Examples

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


Summary

A month-day in the ISO-8601 calendar system, such as --12-03.

Class diagram

final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15

System.out.println(monthDay.getMonth()); // APRIL
System.out.println(monthDay.getMonthValue()); // 4
System.out.println(monthDay.getDayOfMonth()); // 15

Methods

Temporal adjustInto (Temporal temporal)

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

final var monthDay = MonthDay.of(Month.DECEMBER, 15);
System.out.println(monthDay); // --12-15

final var date = LocalDate.of(2100, 2, 3);
System.out.println(date); // 2100-02-03

final var ret = monthDay.adjustInto(date);
System.out.println(ret); // 2100-12-15

LocalDate atYear (int year)

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

final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15

System.out.println(monthDay.atYear(2100)); // 2100-04-15
System.out.println(monthDay.atYear(1800)); // 1800-04-15

int compareTo (MonthDay other)

Compares this month-day to another month-day.

final var monthDay1 = MonthDay.of(Month.JANUARY, 15);
final var monthDay2 = MonthDay.of(Month.JANUARY, 15);

System.out.println(monthDay1); // --01-15
System.out.println(monthDay2); // --01-15

System.out.println(monthDay1.compareTo(monthDay2)); // 0
System.out.println(monthDay2.compareTo(monthDay1)); // 0
final var monthDay1 = MonthDay.of(Month.JANUARY, 15);
final var monthDay2 = MonthDay.of(Month.FEBRUARY, 15);

System.out.println(monthDay1); // --01-15
System.out.println(monthDay2); // --02-15

System.out.println(monthDay1.compareTo(monthDay2)); // -1
System.out.println(monthDay2.compareTo(monthDay1)); // 1
final var monthDay1 = MonthDay.of(Month.APRIL, 2);
final var monthDay2 = MonthDay.of(Month.APRIL, 1);

System.out.println(monthDay1); // --04-02
System.out.println(monthDay2); // --04-01

System.out.println(monthDay1.compareTo(monthDay2)); // 1
System.out.println(monthDay2.compareTo(monthDay1)); // -1

boolean equals (Object obj)

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

final var monthDay1 = MonthDay.of(Month.JANUARY, 15);
final var monthDay2 = MonthDay.of(Month.JANUARY, 15);

System.out.println(monthDay1); // --01-15
System.out.println(monthDay2); // --01-15

System.out.println(monthDay1.equals(monthDay2)); // true
System.out.println(monthDay2.equals(monthDay1)); // true
final var monthDay1 = MonthDay.of(Month.JANUARY, 15);
final var monthDay2 = MonthDay.of(Month.FEBRUARY, 15);

System.out.println(monthDay1); // --01-15
System.out.println(monthDay2); // --02-15

System.out.println(monthDay1.equals(monthDay2)); // false
System.out.println(monthDay2.equals(monthDay1)); // false
final var monthDay1 = MonthDay.of(Month.APRIL, 2);
final var monthDay2 = MonthDay.of(Month.APRIL, 1);

System.out.println(monthDay1); // --04-02
System.out.println(monthDay2); // --04-01

System.out.println(monthDay1.equals(monthDay2)); // false
System.out.println(monthDay2.equals(monthDay1)); // false

String format (DateTimeFormatter formatter)

Formats this month-day using the specified formatter.

final var monthDay = MonthDay.of(Month.APRIL, 1);
System.out.println(monthDay); // --04-01

final var ret1 = monthDay.format(DateTimeFormatter.ofPattern("MM-dd"));
System.out.println(ret1); // 04-01

final var ret2 = monthDay.format(DateTimeFormatter.ofPattern("M-d"));
System.out.println(ret2); // 4-1

static MonthDay from (TemporalAccessor temporal)

Obtains an instance of MonthDay from a temporal object.

final var date = LocalDate.of(2100, 2, 3);
System.out.println(date); // 2100-02-03

final var ret = MonthDay.from(date);
System.out.println(ret); // --02-03
final var dateTime = LocalDateTime.of(1999, 12, 31, 7, 30);
System.out.println(dateTime); // 1999-12-31T07:30

final var ret = MonthDay.from(dateTime);
System.out.println(ret); // --12-31

int get (TemporalField field)

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

final var monthDay = MonthDay.of(Month.JANUARY, 15);
System.out.println(monthDay); // --01-15

System.out.println(monthDay.get(ChronoField.MONTH_OF_YEAR)); // 1
System.out.println(monthDay.get(ChronoField.DAY_OF_MONTH)); // 15
final var monthDay = MonthDay.of(Month.DECEMBER, 31);
System.out.println(monthDay); // --12-31

System.out.println(monthDay.get(ChronoField.MONTH_OF_YEAR)); // 12
System.out.println(monthDay.get(ChronoField.DAY_OF_MONTH)); // 31

int getDayOfMonth ()

Gets the day-of-month field.

final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15

System.out.println(monthDay.getMonth()); // APRIL
System.out.println(monthDay.getMonthValue()); // 4
System.out.println(monthDay.getDayOfMonth()); // 15

long getLong (TemporalField field)

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

final var monthDay = MonthDay.of(Month.JANUARY, 15);
System.out.println(monthDay); // --01-15

System.out.println(monthDay.getLong(ChronoField.MONTH_OF_YEAR)); // 1
System.out.println(monthDay.getLong(ChronoField.DAY_OF_MONTH)); // 15
final var monthDay = MonthDay.of(Month.DECEMBER, 31);
System.out.println(monthDay); // --12-31

System.out.println(monthDay.getLong(ChronoField.MONTH_OF_YEAR)); // 12
System.out.println(monthDay.getLong(ChronoField.DAY_OF_MONTH)); // 31

Month getMonth ()

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

final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15

System.out.println(monthDay.getMonth()); // APRIL
System.out.println(monthDay.getMonthValue()); // 4
System.out.println(monthDay.getDayOfMonth()); // 15

int getMonthValue ()

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

final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15

System.out.println(monthDay.getMonth()); // APRIL
System.out.println(monthDay.getMonthValue()); // 4
System.out.println(monthDay.getDayOfMonth()); // 15

int hashCode ()

A hash code for this month-day.

final var monthDay = MonthDay.of(Month.JANUARY, 15);
System.out.println(monthDay); // --01-15
System.out.println(monthDay.hashCode()); // 79
final var monthDay = MonthDay.of(Month.DECEMBER, 31);
System.out.println(monthDay); // --12-31
System.out.println(monthDay.hashCode()); // 799

boolean isAfter (MonthDay other)

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

final var monthDay = MonthDay.of(Month.FEBRUARY, 1);
System.out.println(monthDay); // --02-01

final var monthDay2 = MonthDay.of(Month.JANUARY, 31);
final var monthDay3 = MonthDay.of(Month.FEBRUARY, 1);
final var monthDay4 = MonthDay.of(Month.FEBRUARY, 2);

System.out.println(monthDay2); // --01-31
System.out.println(monthDay3); // --02-01
System.out.println(monthDay4); // --02-02

System.out.println(monthDay.isAfter(monthDay2)); // true
System.out.println(monthDay.isAfter(monthDay3)); // false
System.out.println(monthDay.isAfter(monthDay4)); // false

boolean isBefore (MonthDay other)

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

final var monthDay = MonthDay.of(Month.FEBRUARY, 1);
System.out.println(monthDay); // --02-01

final var monthDay2 = MonthDay.of(Month.JANUARY, 31);
final var monthDay3 = MonthDay.of(Month.FEBRUARY, 1);
final var monthDay4 = MonthDay.of(Month.FEBRUARY, 2);

System.out.println(monthDay2); // --01-31
System.out.println(monthDay3); // --02-01
System.out.println(monthDay4); // --02-02

System.out.println(monthDay.isBefore(monthDay2)); // true
System.out.println(monthDay.isBefore(monthDay3)); // false
System.out.println(monthDay.isBefore(monthDay4)); // false

boolean isSupported (TemporalField field)

Checks if the specified field is supported.

final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15

System.out.println(monthDay.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(monthDay.isSupported(ChronoField.DAY_OF_MONTH)); // true

System.out.println(monthDay.isSupported(ChronoField.YEAR)); // false

boolean isValidYear (int year)

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

final var monthDay = MonthDay.of(Month.FEBRUARY, 29);
System.out.println(monthDay); // --02-29

System.out.println(Year.isLeap(2103)); // false
System.out.println(monthDay.isValidYear(2103)); // false

System.out.println(Year.isLeap(2104)); // true
System.out.println(monthDay.isValidYear(2104)); // true

static MonthDay now ()

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

final var now = MonthDay.now();
System.out.println(now); // --10-19

static MonthDay now (Clock clock)

Obtains the current month-day from the specified clock.

// A clock advanced 5 days.
final var clock = Clock.offset(Clock.systemDefaultZone(), Duration.ofDays(5));

System.out.println(MonthDay.now()); // --10-19
System.out.println(MonthDay.now(clock)); // --10-24

static MonthDay now (ZoneId zone)

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

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

final var now = MonthDay.now(zone);
System.out.println(now); // --10-19

static MonthDay of (int month, int dayOfMonth)

Obtains an instance of MonthDay.

final var monthDay = MonthDay.of(1, 2);
System.out.println(monthDay.getMonth()); // JANUARY
System.out.println(monthDay.getMonthValue()); // 1
System.out.println(monthDay.getDayOfMonth()); // 2
final var monthDay = MonthDay.of(12, 15);
System.out.println(monthDay.getMonth()); // DECEMBER
System.out.println(monthDay.getMonthValue()); // 12
System.out.println(monthDay.getDayOfMonth()); // 15

static MonthDay of (Month month, int dayOfMonth)

Obtains an instance of MonthDay.

final var monthDay = MonthDay.of(Month.JANUARY, 2);
System.out.println(monthDay.getMonth()); // JANUARY
System.out.println(monthDay.getMonthValue()); // 1
System.out.println(monthDay.getDayOfMonth()); // 2
final var monthDay = MonthDay.of(Month.DECEMBER, 15);
System.out.println(monthDay.getMonth()); // DECEMBER
System.out.println(monthDay.getMonthValue()); // 12
System.out.println(monthDay.getDayOfMonth()); // 15

static MonthDay parse (CharSequence text)

Obtains an instance of MonthDay from a text string such as --12-03.

final var monthDay1 = MonthDay.parse("--04-05");
System.out.println(monthDay1); // --04-05

final var monthDay2 = MonthDay.parse("--12-31");
System.out.println(monthDay2); // --12-31

static MonthDay parse (CharSequence text, DateTimeFormatter formatter)

Obtains an instance of MonthDay from a text string using a specific formatter.

final var monthDay1 = MonthDay.parse(
        "04-05", DateTimeFormatter.ofPattern("MM-dd"));
System.out.println(monthDay1); // --04-05

final var monthDay2 = MonthDay.parse(
        "8-1", DateTimeFormatter.ofPattern("M-d"));
System.out.println(monthDay2); // --08-01

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

Queries this month-day using the specified query.

final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15

final var ret = monthDay.query(TemporalQueries.chronology());
System.out.println(ret); // ISO

ValueRange range (TemporalField field)

Gets the range of valid values for the specified field.

final var monthDay = MonthDay.of(Month.JANUARY, 1);
System.out.println(monthDay); // --01-01

System.out.println(monthDay.range(ChronoField.MONTH_OF_YEAR)); // 1 - 12
System.out.println(monthDay.range(ChronoField.DAY_OF_MONTH)); // 1 - 31
final var monthDay = MonthDay.of(Month.FEBRUARY, 1);
System.out.println(monthDay); // --02-01

System.out.println(monthDay.range(ChronoField.MONTH_OF_YEAR)); // 1 - 12
System.out.println(monthDay.range(ChronoField.DAY_OF_MONTH)); // 1 - 28/29

String toString ()

Outputs this month-day as a String, such as --12-03.

final var monthDay = MonthDay.of(Month.JANUARY, 15);
final var str = monthDay.toString();
System.out.println(str); // --01-15
final var monthDay = MonthDay.of(Month.DECEMBER, 31);
final var str = monthDay.toString();
System.out.println(str); // --12-31

MonthDay with (Month month)

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

final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15

final var ret1 = monthDay.with(Month.DECEMBER);
System.out.println(ret1); // --12-15

final var ret2 = monthDay.withDayOfMonth(30);
System.out.println(ret2); // --04-30

final var ret3 = monthDay.withMonth(1);
System.out.println(ret3); // --01-15

MonthDay withDayOfMonth (int dayOfMonth)

Returns a copy of this MonthDay with the day-of-month altered.

final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15

final var ret1 = monthDay.with(Month.DECEMBER);
System.out.println(ret1); // --12-15

final var ret2 = monthDay.withDayOfMonth(30);
System.out.println(ret2); // --04-30

final var ret3 = monthDay.withMonth(1);
System.out.println(ret3); // --01-15

MonthDay withMonth (int month)

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

final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15

final var ret1 = monthDay.with(Month.DECEMBER);
System.out.println(ret1); // --12-15

final var ret2 = monthDay.withDayOfMonth(30);
System.out.println(ret2); // --04-30

final var ret3 = monthDay.withMonth(1);
System.out.println(ret3); // --01-15

Related posts

To top of page