Java : MonthDay (月・日) - API使用例
MonthDay (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
MonthDay は、日付の月と日を表します。
例えば、
- 4月15日
- 12月31日
などが表現できます。
オブジェクトの生成には of メソッドを使います。
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
メソッド
Temporal adjustInto (Temporal temporal)
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)
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)
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)
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)
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)
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)
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 ()
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)
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 ()
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 ()
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 ()
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)
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)
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)
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)
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 ()
final var now = MonthDay.now();
System.out.println(now); // --10-19
static MonthDay now (Clock clock)
// 意図的に5日進めたClock
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)
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)
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)
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)
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)
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)
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)
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 ()
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)
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)
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)
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
関連記事
- 日付・時刻の基本
- Date, CalendarではなくLocalDateTime, ZonedDateTimeを使おう
- 文字列と日付・時刻の変換
- 日付と時刻、曜日の計算
- 現在時刻(日時)の取得いろいろ
- 現在の曜日(DayOfWeek)を取得
- ZoneIdとZoneOffsetの違い
- API 使用例
- Calendar (カレンダー)
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock (時計)
- Date (日付・時刻)
- DateTimeException (日付・時刻の例外)
- DateTimeParseException (日付・時刻の解析例外)
- DayOfWeek (曜日)
- Duration (時間の量)
- Era (紀元)
- Instant (時点)
- InstantSource
- JapaneseDate (和暦を使った日付)
- LocalDate (日付・タイムゾーンなし)
- LocalDateTime (日時・タイムゾーンなし)
- LocalTime (時刻・タイムゾーンなし)
- Month (月)
- OffsetDateTime (日時・オフセットあり)
- OffsetTime (時刻・オフセットあり)
- Period (日付の量)
- Temporal
- TemporalAccessor
- TemporalAdjuster (日付・時刻の調整)
- TemporalAdjusters (日付・時刻の調整ユーティリティ)
- TimeZone (タイムゾーン)
- Year (年)
- YearMonth (年・月)
- ZonedDateTime (日時・タイムゾーンあり)
- ZoneId (タイムゾーンID)
- ZoneOffset (タイムゾーン・オフセット)