Java : Temporal - API使用例
Temporal (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
Temporal インタフェースを使うと、日付や時刻に対して汎用的な操作ができます。
ただし、フレームワークレベルのインタフェースであるため直接使うことは少ないと思います。
final Temporal temporal = LocalDate.of(2100, 4, 10);
System.out.println(temporal); // 2100-04-10
final var ret1 = temporal.plus(Period.ofDays(5));
System.out.println(ret1); // 2100-04-15
final var ret2 = temporal.minus(Period.ofYears(101));
System.out.println(ret2); // 1999-04-10
final Temporal temporal = LocalTime.of(16, 59);
System.out.println(temporal); // 16:59
final var ret1 = temporal.plus(Duration.ofMinutes(1));
System.out.println(ret1); // 17:00
final var ret2 = temporal.minus(Duration.ofHours(10));
System.out.println(ret2); // 06:59
メソッド
boolean isSupported (TemporalUnit unit)
final Temporal temporal = LocalDate.of(2100, 4, 15);
System.out.println(temporal); // 2100-04-15
System.out.println(temporal.isSupported(ChronoField.YEAR)); // true
System.out.println(temporal.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(temporal.isSupported(ChronoField.DAY_OF_MONTH)); // true
System.out.println(temporal.isSupported(ChronoField.HOUR_OF_DAY)); // false
final Temporal temporal = LocalTime.of(16, 30, 59);
System.out.println(temporal); // 16:30:59
System.out.println(temporal.isSupported(ChronoField.HOUR_OF_DAY)); // true
System.out.println(temporal.isSupported(ChronoField.MINUTE_OF_HOUR)); // true
System.out.println(temporal.isSupported(ChronoField.SECOND_OF_MINUTE)); // true
System.out.println(temporal.isSupported(ChronoField.YEAR)); // false
default Temporal minus (long amountToSubtract, TemporalUnit unit)
final Temporal temporal = LocalDate.of(2100, 4, 10);
System.out.println(temporal); // 2100-04-10
final var ret1 = temporal.plus(5, ChronoUnit.DAYS);
System.out.println(ret1); // 2100-04-15
final var ret2 = temporal.minus(101, ChronoUnit.YEARS);
System.out.println(ret2); // 1999-04-10
final Temporal temporal = LocalTime.of(16, 59);
System.out.println(temporal); // 16:59
final var ret1 = temporal.plus(1, ChronoUnit.MINUTES);
System.out.println(ret1); // 17:00
final var ret2 = temporal.minus(10, ChronoUnit.HOURS);
System.out.println(ret2); // 06:59
default Temporal minus (TemporalAmount amount)
final Temporal temporal = LocalDate.of(2100, 4, 10);
System.out.println(temporal); // 2100-04-10
final var ret1 = temporal.plus(Period.ofDays(5));
System.out.println(ret1); // 2100-04-15
final var ret2 = temporal.minus(Period.ofYears(101));
System.out.println(ret2); // 1999-04-10
final Temporal temporal = LocalTime.of(16, 59);
System.out.println(temporal); // 16:59
final var ret1 = temporal.plus(Duration.ofMinutes(1));
System.out.println(ret1); // 17:00
final var ret2 = temporal.minus(Duration.ofHours(10));
System.out.println(ret2); // 06:59
Temporal plus (long amountToAdd, TemporalUnit unit)
final Temporal temporal = LocalDate.of(2100, 4, 10);
System.out.println(temporal); // 2100-04-10
final var ret1 = temporal.plus(5, ChronoUnit.DAYS);
System.out.println(ret1); // 2100-04-15
final var ret2 = temporal.minus(101, ChronoUnit.YEARS);
System.out.println(ret2); // 1999-04-10
final Temporal temporal = LocalTime.of(16, 59);
System.out.println(temporal); // 16:59
final var ret1 = temporal.plus(1, ChronoUnit.MINUTES);
System.out.println(ret1); // 17:00
final var ret2 = temporal.minus(10, ChronoUnit.HOURS);
System.out.println(ret2); // 06:59
default Temporal plus (TemporalAmount amount)
final Temporal temporal = LocalDate.of(2100, 4, 10);
System.out.println(temporal); // 2100-04-10
final var ret1 = temporal.plus(Period.ofDays(5));
System.out.println(ret1); // 2100-04-15
final var ret2 = temporal.minus(Period.ofYears(101));
System.out.println(ret2); // 1999-04-10
final Temporal temporal = LocalTime.of(16, 59);
System.out.println(temporal); // 16:59
final var ret1 = temporal.plus(Duration.ofMinutes(1));
System.out.println(ret1); // 17:00
final var ret2 = temporal.minus(Duration.ofHours(10));
System.out.println(ret2); // 06:59
long until (Temporal endExclusive, TemporalUnit unit)
final Temporal temporal1 = LocalTime.of(8, 0);
final Temporal temporal2 = LocalTime.of(10, 30);
System.out.println(temporal1); // 08:00
System.out.println(temporal2); // 10:30
System.out.println(temporal1.until(temporal2, ChronoUnit.HOURS)); // 2
System.out.println(temporal1.until(temporal2, ChronoUnit.MINUTES)); // 150
System.out.println(temporal2.until(temporal1, ChronoUnit.HOURS)); // -2
System.out.println(temporal2.until(temporal1, ChronoUnit.MINUTES)); // -150
default Temporal with (TemporalAdjuster adjuster)
final Temporal temporal = LocalDate.of(2100, 4, 15);
System.out.println(temporal); // 2100-04-15
final var ret1 = temporal.with(Year.of(1999));
System.out.println(ret1); // 1999-04-15
final var ret2 = temporal.with(Month.JANUARY);
System.out.println(ret2); // 2100-01-15
final var ret3 = temporal.with(TemporalAdjusters.firstDayOfMonth());
System.out.println(ret3); // 2100-04-01
final var ret4 = temporal.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(ret4); // 2100-04-30
Temporal with (TemporalField field, long newValue)
final Temporal temporal = LocalDate.of(2100, 4, 15);
System.out.println(temporal); // 2100-04-15
final var ret1 = temporal.with(ChronoField.YEAR, 1999);
System.out.println(ret1); // 1999-04-15
final var ret2 = temporal.with(ChronoField.MONTH_OF_YEAR, 1);
System.out.println(ret2); // 2100-01-15
final var ret3 = temporal.with(ChronoField.DAY_OF_MONTH, 30);
System.out.println(ret3); // 2100-04-30
TemporalAccessorで宣言されたメソッド
get, getLong, isSupported, query, range
「Java API 使用例 : TemporalAccessor」をご参照ください。
関連記事
- 日付・時刻の基本
- 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 (日付の量)
- TemporalAccessor
- TemporalAdjuster (日付・時刻の調整)
- TemporalAdjusters (日付・時刻の調整ユーティリティ)
- TimeZone (タイムゾーン)
- Year (年)
- YearMonth (年・月)
- ZonedDateTime (日時・タイムゾーンあり)
- ZoneId (タイムゾーンID)
- ZoneOffset (タイムゾーン・オフセット)