Java : TemporalAdjusters (日付・時刻の調整ユーティリティ) - API使用例
TemporalAdjusters (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
TemporalAdjusters は、用途別の TemporalAdjuster を返すユーティリティクラスです。
例えば、月の最終日に調整する TemporalAdjuster は、lastDayOfMonth() メソッドで取得します。
final var adjuster = TemporalAdjusters.lastDayOfMonth();
final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01
System.out.println(date1.with(adjuster)); // 2100-01-31
final var date2 = LocalDate.of(2100, 2, 15);
System.out.println(date2); // 2100-02-15
System.out.println(date2.with(adjuster)); // 2100-02-28
final var dateTime = LocalDateTime.of(2100, 4, 2, 12, 30);
System.out.println(dateTime); // 2100-04-02T12:30
System.out.println(dateTime.with(adjuster)); // 2100-04-30T12:30
メソッド
static TemporalAdjuster dayOfWeekInMonth (int ordinal, DayOfWeek dayOfWeek)
final var formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日(E)");
final var date = LocalDate.of(2100, 12, 1);
System.out.println(date.format(formatter)); // 2100年12月01日(水)
{
final var ret1 = date.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.WEDNESDAY));
System.out.println(ret1.format(formatter)); // 2100年12月01日(水)
final var ret2 = date.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.THURSDAY));
System.out.println(ret2.format(formatter)); // 2100年12月02日(木)
final var ret3 = date.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.FRIDAY));
System.out.println(ret3.format(formatter)); // 2100年12月03日(金)
}
{
final var ret1 = date.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.SUNDAY));
System.out.println(ret1.format(formatter)); // 2100年12月05日(日)
final var ret2 = date.with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.SUNDAY));
System.out.println(ret2.format(formatter)); // 2100年12月12日(日)
final var ret3 = date.with(TemporalAdjusters.dayOfWeekInMonth(3, DayOfWeek.SUNDAY));
System.out.println(ret3.format(formatter)); // 2100年12月19日(日)
}
static TemporalAdjuster firstDayOfMonth ()
final var adjuster = TemporalAdjusters.firstDayOfMonth();
final var date1 = LocalDate.of(2100, 1, 15);
System.out.println(date1); // 2100-01-15
System.out.println(date1.with(adjuster)); // 2100-01-01
final var date2 = LocalDate.of(2100, 2, 28);
System.out.println(date2); // 2100-02-28
System.out.println(date2.with(adjuster)); // 2100-02-01
final var date3 = LocalDate.of(1999, 12, 31);
System.out.println(date3); // 1999-12-31
System.out.println(date3.with(adjuster)); // 1999-12-01
static TemporalAdjuster firstDayOfNextMonth ()
final var adjuster = TemporalAdjusters.firstDayOfNextMonth();
final var date1 = LocalDate.of(2100, 1, 15);
System.out.println(date1); // 2100-01-15
System.out.println(date1.with(adjuster)); // 2100-02-01
final var date2 = LocalDate.of(2100, 2, 28);
System.out.println(date2); // 2100-02-28
System.out.println(date2.with(adjuster)); // 2100-03-01
final var date3 = LocalDate.of(1999, 12, 31);
System.out.println(date3); // 1999-12-31
System.out.println(date3.with(adjuster)); // 2000-01-01
static TemporalAdjuster firstDayOfNextYear ()
final var adjuster = TemporalAdjusters.firstDayOfNextYear();
final var date1 = LocalDate.of(2100, 1, 15);
System.out.println(date1); // 2100-01-15
System.out.println(date1.with(adjuster)); // 2101-01-01
final var date2 = LocalDate.of(2100, 2, 28);
System.out.println(date2); // 2100-02-28
System.out.println(date2.with(adjuster)); // 2101-01-01
final var date3 = LocalDate.of(1999, 12, 31);
System.out.println(date3); // 1999-12-31
System.out.println(date3.with(adjuster)); // 2000-01-01
static TemporalAdjuster firstDayOfYear ()
final var adjuster = TemporalAdjusters.firstDayOfYear();
final var date1 = LocalDate.of(2100, 1, 15);
System.out.println(date1); // 2100-01-15
System.out.println(date1.with(adjuster)); // 2100-01-01
final var date2 = LocalDate.of(2100, 2, 28);
System.out.println(date2); // 2100-02-28
System.out.println(date2.with(adjuster)); // 2100-01-01
final var date3 = LocalDate.of(1999, 12, 31);
System.out.println(date3); // 1999-12-31
System.out.println(date3.with(adjuster)); // 1999-01-01
static TemporalAdjuster firstInMonth (DayOfWeek dayOfWeek)
final var formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日(E)");
final var date = LocalDate.of(2100, 12, 1);
System.out.println(date.format(formatter)); // 2100年12月01日(水)
final var ret1 = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // 2100年12月06日(月)
final var ret2 = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // 2100年12月07日(火)
final var ret3 = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // 2100年12月01日(水)
final var ret4 = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // 2100年12月02日(木)
final var ret5 = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // 2100年12月03日(金)
static TemporalAdjuster lastDayOfMonth ()
final var adjuster = TemporalAdjusters.lastDayOfMonth();
final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01
System.out.println(date1.with(adjuster)); // 2100-01-31
final var date2 = LocalDate.of(2100, 2, 15);
System.out.println(date2); // 2100-02-15
System.out.println(date2.with(adjuster)); // 2100-02-28
final var date3 = LocalDate.of(1999, 4, 3);
System.out.println(date3); // 1999-04-03
System.out.println(date3.with(adjuster)); // 1999-04-30
static TemporalAdjuster lastDayOfYear ()
final var adjuster = TemporalAdjusters.lastDayOfYear();
final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01
System.out.println(date1.with(adjuster)); // 2100-12-31
final var date2 = LocalDate.of(2100, 2, 15);
System.out.println(date2); // 2100-02-15
System.out.println(date2.with(adjuster)); // 2100-12-31
final var date3 = LocalDate.of(1999, 12, 3);
System.out.println(date3); // 1999-12-03
System.out.println(date3.with(adjuster)); // 1999-12-31
static TemporalAdjuster lastInMonth (DayOfWeek dayOfWeek)
final var formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日(E)");
final var date = LocalDate.of(2100, 6, 1);
System.out.println(date.format(formatter)); // 2100年06月01日(火)
final var ret1 = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // 2100年06月28日(月)
final var ret2 = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // 2100年06月29日(火)
final var ret3 = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // 2100年06月30日(水)
final var ret4 = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // 2100年06月24日(木)
final var ret5 = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // 2100年06月25日(金)
static TemporalAdjuster next (DayOfWeek dayOfWeek)
final var formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日(E)");
final var date = LocalDate.of(2100, 12, 15);
System.out.println(date.format(formatter)); // 2100年12月15日(水)
final var ret1 = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // 2100年12月20日(月)
final var ret2 = date.with(TemporalAdjusters.next(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // 2100年12月21日(火)
final var ret3 = date.with(TemporalAdjusters.next(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // 2100年12月22日(水)
final var ret4 = date.with(TemporalAdjusters.next(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // 2100年12月16日(木)
final var ret5 = date.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // 2100年12月17日(金)
static TemporalAdjuster nextOrSame (DayOfWeek dayOfWeek)
final var formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日(E)");
final var date = LocalDate.of(2100, 12, 15);
System.out.println(date.format(formatter)); // 2100年12月15日(水)
final var ret1 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // 2100年12月20日(月)
final var ret2 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // 2100年12月21日(火)
final var ret3 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // 2100年12月15日(水)
final var ret4 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // 2100年12月16日(木)
final var ret5 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // 2100年12月17日(金)
static TemporalAdjuster ofDateAdjuster (UnaryOperator<LocalDate> dateBasedAdjuster)
final var adjuster = TemporalAdjusters.ofDateAdjuster(date -> {
return date.plusDays(2);
});
final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01
System.out.println(date1.with(adjuster)); // 2100-01-03
final var date2 = LocalDate.of(1999, 12, 31);
System.out.println(date2); // 1999-12-31
System.out.println(date2.with(adjuster)); // 2000-01-02
static TemporalAdjuster previous (DayOfWeek dayOfWeek)
final var formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日(E)");
final var date = LocalDate.of(2100, 12, 15);
System.out.println(date.format(formatter)); // 2100年12月15日(水)
final var ret1 = date.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // 2100年12月13日(月)
final var ret2 = date.with(TemporalAdjusters.previous(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // 2100年12月14日(火)
final var ret3 = date.with(TemporalAdjusters.previous(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // 2100年12月08日(水)
final var ret4 = date.with(TemporalAdjusters.previous(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // 2100年12月09日(木)
final var ret5 = date.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // 2100年12月10日(金)
static TemporalAdjuster previousOrSame (DayOfWeek dayOfWeek)
final var formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日(E)");
final var date = LocalDate.of(2100, 12, 15);
System.out.println(date.format(formatter)); // 2100年12月15日(水)
final var ret1 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // 2100年12月13日(月)
final var ret2 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // 2100年12月14日(火)
final var ret3 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // 2100年12月15日(水)
final var ret4 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // 2100年12月09日(木)
final var ret5 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // 2100年12月10日(金)
関連記事
- 日付・時刻の基本
- 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 (月)
- MonthDay (月・日)
- OffsetDateTime (日時・オフセットあり)
- OffsetTime (時刻・オフセットあり)
- Period (日付の量)
- Temporal
- TemporalAccessor
- TemporalAdjuster (日付・時刻の調整)
- TimeZone (タイムゾーン)
- Year (年)
- YearMonth (年・月)
- ZonedDateTime (日時・タイムゾーンあり)
- ZoneId (タイムゾーンID)
- ZoneOffset (タイムゾーン・オフセット)