Java : OffsetDateTime (日時・オフセットあり) - API使用例
OffsetDateTime (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
OffsetDateTime は日時を表すクラスです。
日時は、
- 日付 (年と月と日)
- 時刻 (時と分と秒とナノ秒)
で構成されます。
また、時差となるオフセット (ZoneOffset) を持ちます。
ZoneOffset は地理的な地域ごとのタイムゾーン情報は持ちません。単純な時差のみとなります。
もしタイムゾーンを持った日時を使いたい場合は、代わりに ZonedDateTime を使いましょう。
OffsetDateTime のインスタンスの生成には of メソッドを使います。
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00
final var dateTime = OffsetDateTime.of(2100, 4, 14, 13, 30, 15, 123000000, offset);
System.out.println(dateTime); // 2100-04-14T13:30:15.123+04:00
System.out.println(dateTime.getYear()); // 2100
System.out.println(dateTime.getMonth()); // APRIL
System.out.println(dateTime.getDayOfMonth()); // 14
System.out.println(dateTime.getHour()); // 13
System.out.println(dateTime.getMinute()); // 30
System.out.println(dateTime.getSecond()); // 15
System.out.println(dateTime.getNano()); // 123000000
System.out.println(dateTime.getOffset()); // +04:00
フィールド
static final OffsetDateTime MAX
System.out.println(OffsetDateTime.MAX); // +999999999-12-31T23:59:59.999999999-18:00
static final OffsetDateTime MIN
System.out.println(OffsetDateTime.MIN); // -999999999-01-01T00:00+18:00
メソッド
Temporal adjustInto (Temporal temporal)
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var temporal = ZonedDateTime.of(2100, 8, 10, 12, 30, 45, 0, zone);
System.out.println(temporal); // 2100-08-10T12:30:45-01:00[Etc/GMT+1]
final var dateTime = OffsetDateTime.of(1800, 10, 20, 1, 2, 3, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 1800-10-20T01:02:03Z
final var ret = dateTime.adjustInto(temporal);
System.out.println(ret); // 1800-10-20T01:02:03-01:00[Etc/GMT+1]
ZonedDateTime atZoneSameInstant (ZoneId zone)
final var datTime = OffsetDateTime.of(2100, 1, 2, 12, 0, 0, 0, ZoneOffset.UTC);
System.out.println(datTime); // 2100-01-02T12:00Z
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var ret = datTime.atZoneSameInstant(zone);
System.out.println(ret); // 2100-01-02T11:00-01:00[Etc/GMT+1]
final var offset = ZoneOffset.ofHours(9);
System.out.println(offset); // +09:00
final var datTime = OffsetDateTime.of(2100, 1, 2, 12, 0, 0, 0, offset);
System.out.println(datTime); // 2100-01-02T12:00+09:00
final var ret = datTime.atZoneSameInstant(ZoneOffset.UTC);
System.out.println(ret); // 2100-01-02T03:00Z
ZonedDateTime atZoneSimilarLocal (ZoneId zone)
final var datTime = OffsetDateTime.of(2100, 1, 2, 12, 0, 0, 0, ZoneOffset.UTC);
System.out.println(datTime); // 2100-01-02T12:00Z
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var ret = datTime.atZoneSimilarLocal(zone);
System.out.println(ret); // 2100-01-02T12:00-01:00[Etc/GMT+1]
final var offset = ZoneOffset.ofHours(9);
System.out.println(offset); // +09:00
final var datTime = OffsetDateTime.of(2100, 1, 2, 12, 0, 0, 0, offset);
System.out.println(datTime); // 2100-01-02T12:00+09:00
final var ret = datTime.atZoneSimilarLocal(ZoneOffset.UTC);
System.out.println(ret); // 2100-01-02T12:00Z
int compareTo (OffsetDateTime other)
final var dateTime1 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-01-01T00:00Z
System.out.println(dateTime2); // 2100-01-01T00:00Z
System.out.println(dateTime1.compareTo(dateTime2)); // 0
final var offset1 = ZoneOffset.ofHours(2);
final var offset2 = ZoneOffset.ofHours(-4);
final var dateTime1 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, offset1);
final var dateTime2 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, offset2);
System.out.println(dateTime1); // 2100-01-01T00:00+02:00
System.out.println(dateTime2); // 2100-01-01T00:00-04:00
System.out.println(dateTime1.compareTo(dateTime2)); // -1
final var dateTime1 = OffsetDateTime.of(2100, 4, 14, 15, 20, 0, 0, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 4, 14, 15, 21, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-04-14T15:20Z
System.out.println(dateTime2); // 2100-04-14T15:21Z
System.out.println(dateTime1.compareTo(dateTime2)); // -1
final var dateTime1 = OffsetDateTime.of(2100, 4, 15, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 4, 14, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-04-15T00:00Z
System.out.println(dateTime2); // 2100-04-14T00:00Z
System.out.println(dateTime1.compareTo(dateTime2)); // 1
final var dateTime1 = OffsetDateTime.of(2100, 4, 13, 0, 0, 0, 123, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 4, 13, 0, 0, 0, 456, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-04-13T00:00:00.000000123Z
System.out.println(dateTime2); // 2100-04-13T00:00:00.000000456Z
System.out.println(dateTime1.compareTo(dateTime2)); // -1
boolean equals (Object obj)
final var dateTime1 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-01-01T00:00Z
System.out.println(dateTime2); // 2100-01-01T00:00Z
System.out.println(dateTime1.equals(dateTime2)); // true
final var offset1 = ZoneOffset.ofHours(2);
final var offset2 = ZoneOffset.ofHours(-4);
final var dateTime1 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, offset1);
final var dateTime2 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, offset2);
System.out.println(dateTime1); // 2100-01-01T00:00+02:00
System.out.println(dateTime2); // 2100-01-01T00:00-04:00
System.out.println(dateTime1.equals(dateTime2)); // false
final var dateTime1 = OffsetDateTime.of(2100, 4, 14, 15, 20, 0, 0, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 4, 14, 15, 21, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-04-14T15:20Z
System.out.println(dateTime2); // 2100-04-14T15:21Z
System.out.println(dateTime1.equals(dateTime2)); // false
final var dateTime1 = OffsetDateTime.of(2100, 4, 15, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 4, 14, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-04-15T00:00Z
System.out.println(dateTime2); // 2100-04-14T00:00Z
System.out.println(dateTime1.equals(dateTime2)); // false
final var dateTime1 = OffsetDateTime.of(2100, 4, 13, 0, 0, 0, 123, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 4, 13, 0, 0, 0, 456, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-04-13T00:00:00.000000123Z
System.out.println(dateTime2); // 2100-04-13T00:00:00.000000456Z
System.out.println(dateTime1.equals(dateTime2)); // false
String format (DateTimeFormatter formatter)
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00
final var dateTime = OffsetDateTime.of(2099, 4, 7, 13, 30, 45, 0, offset);
System.out.println(dateTime); // 2099-04-07T13:30:45+04:00
{
final var ret1 = dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
System.out.println(ret1); // 2099-04-07T13:30:45+04:00
final var ret2 = dateTime.format(
DateTimeFormatter.ofPattern("yyyy年MM月dd日(E) HH時mm分ss秒 O"));
System.out.println(ret2); // 2099年04月07日(火) 13時30分45秒 GMT+4
}
{
System.out.println(Locale.getDefault().toLanguageTag()); // ja-JP
final var ret1 = dateTime.format(
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
System.out.println(ret1); // 2099/04/07 13:30:45
final var ret2 = dateTime.format(
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT));
System.out.println(ret2); // 2099/04/07 13:30
try {
final var ret3 = dateTime.format(
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG));
} catch (DateTimeException e) {
System.out.println("DateTimeException! : " + e.getMessage());
}
// 結果
// ↓
//DateTimeException! : Unable to extract ZoneId from temporal 2099-04-07T13:30:45+04:00
}
static OffsetDateTime from (TemporalAccessor temporal)
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var temporal = ZonedDateTime.of(2100, 1, 2, 12, 30, 0, 0, zone);
System.out.println(temporal); // 2100-01-02T12:30-01:00[Etc/GMT+1]
final var ret = OffsetDateTime.from(temporal);
System.out.println(ret); // 2100-01-02T12:30-01:00
int get (TemporalField field)
final var offset = ZoneOffset.ofHours(1);
System.out.println(offset); // +01:00
final var dateTime = OffsetDateTime.of(2100, 4, 13, 12, 30, 45, 123, offset);
System.out.println(dateTime); // 2100-04-13T12:30:45.000000123+01:00
System.out.println(dateTime.get(ChronoField.YEAR)); // 2100
System.out.println(dateTime.get(ChronoField.MONTH_OF_YEAR)); // 4
System.out.println(dateTime.get(ChronoField.DAY_OF_MONTH)); // 13
System.out.println(dateTime.get(ChronoField.HOUR_OF_DAY)); // 12
System.out.println(dateTime.get(ChronoField.MINUTE_OF_HOUR)); // 30
System.out.println(dateTime.get(ChronoField.SECOND_OF_MINUTE)); // 45
System.out.println(dateTime.get(ChronoField.NANO_OF_SECOND)); // 123
System.out.println(dateTime.get(ChronoField.OFFSET_SECONDS)); // 3600
int getDayOfMonth ()
final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
System.out.println(dateTime.getDayOfMonth()); // 1
final var dateTime = OffsetDateTime.of(2100, 1, 2, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-02T00:00Z
System.out.println(dateTime.getDayOfMonth()); // 2
final var dateTime = OffsetDateTime.of(2100, 1, 3, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-03T00:00Z
System.out.println(dateTime.getDayOfMonth()); // 3
final var dateTime = OffsetDateTime.of(2100, 1, 30, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-30T00:00Z
System.out.println(dateTime.getDayOfMonth()); // 30
final var dateTime = OffsetDateTime.of(2100, 1, 31, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-31T00:00Z
System.out.println(dateTime.getDayOfMonth()); // 31
final var dateTime = OffsetDateTime.of(2100, 2, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-02-01T00:00Z
System.out.println(dateTime.getDayOfMonth()); // 1
DayOfWeek getDayOfWeek ()
final var dateTime = OffsetDateTime.of(2100, 1, 3, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-03T00:00Z
System.out.println(dateTime.getDayOfWeek()); // SUNDAY
final var dateTime = OffsetDateTime.of(2100, 1, 4, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-04T00:00Z
System.out.println(dateTime.getDayOfWeek()); // MONDAY
final var dateTime = OffsetDateTime.of(2100, 1, 5, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-05T00:00Z
System.out.println(dateTime.getDayOfWeek()); // TUESDAY
final var dateTime = OffsetDateTime.of(2100, 1, 6, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-06T00:00Z
System.out.println(dateTime.getDayOfWeek()); // WEDNESDAY
final var dateTime = OffsetDateTime.of(2100, 1, 7, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-07T00:00Z
System.out.println(dateTime.getDayOfWeek()); // THURSDAY
final var dateTime = OffsetDateTime.of(2100, 1, 8, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-08T00:00Z
System.out.println(dateTime.getDayOfWeek()); // FRIDAY
final var dateTime = OffsetDateTime.of(2100, 1, 9, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-09T00:00Z
System.out.println(dateTime.getDayOfWeek()); // SATURDAY
final var dateTime = OffsetDateTime.of(2100, 1, 10, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-10T00:00Z
System.out.println(dateTime.getDayOfWeek()); // SUNDAY
int getDayOfYear ()
final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
System.out.println(dateTime.getDayOfYear()); // 1
final var dateTime = OffsetDateTime.of(2100, 1, 2, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-02T00:00Z
System.out.println(dateTime.getDayOfYear()); // 2
final var dateTime = OffsetDateTime.of(2100, 1, 30, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-30T00:00Z
System.out.println(dateTime.getDayOfYear()); // 30
final var dateTime = OffsetDateTime.of(2100, 1, 31, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-31T00:00Z
System.out.println(dateTime.getDayOfYear()); // 31
final var dateTime = OffsetDateTime.of(2100, 2, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-02-01T00:00Z
System.out.println(dateTime.getDayOfYear()); // 32
final var dateTime = OffsetDateTime.of(2100, 2, 2, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-02-02T00:00Z
System.out.println(dateTime.getDayOfYear()); // 33
final var dateTime = OffsetDateTime.of(2100, 12, 31, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-12-31T00:00Z
System.out.println(dateTime.getDayOfYear()); // 365
final var dateTime = OffsetDateTime.of(2101, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2101-01-01T00:00Z
System.out.println(dateTime.getDayOfYear()); // 1
int getHour ()
final var dateTime = OffsetDateTime.of(2100, 1, 1, 14, 30, 15, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T14:30:15Z
System.out.println(dateTime.getHour()); // 14
long getLong (TemporalField field)
final var offset = ZoneOffset.ofHours(1);
System.out.println(offset); // +01:00
final var dateTime = OffsetDateTime.of(2100, 4, 13, 12, 30, 45, 123, offset);
System.out.println(dateTime); // 2100-04-13T12:30:45.000000123+01:00
System.out.println(dateTime.getLong(ChronoField.YEAR)); // 2100
System.out.println(dateTime.getLong(ChronoField.MONTH_OF_YEAR)); // 4
System.out.println(dateTime.getLong(ChronoField.DAY_OF_MONTH)); // 13
System.out.println(dateTime.getLong(ChronoField.HOUR_OF_DAY)); // 12
System.out.println(dateTime.getLong(ChronoField.MINUTE_OF_HOUR)); // 30
System.out.println(dateTime.getLong(ChronoField.SECOND_OF_MINUTE)); // 45
System.out.println(dateTime.getLong(ChronoField.NANO_OF_SECOND)); // 123
System.out.println(dateTime.getLong(ChronoField.OFFSET_SECONDS)); // 3600
int getMinute ()
final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:15Z
System.out.println(dateTime.getMinute()); // 30
Month getMonth ()
final var dateTime = OffsetDateTime.of(2100, 1, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-15T00:00Z
System.out.println(dateTime.getMonth()); // JANUARY
final var dateTime = OffsetDateTime.of(2100, 2, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-02-15T00:00Z
System.out.println(dateTime.getMonth()); // FEBRUARY
final var dateTime = OffsetDateTime.of(2100, 3, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-03-15T00:00Z
System.out.println(dateTime.getMonth()); // MARCH
final var dateTime = OffsetDateTime.of(2100, 12, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-12-15T00:00Z
System.out.println(dateTime.getMonth()); // DECEMBER
final var dateTime = OffsetDateTime.of(2101, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2101-01-01T00:00Z
System.out.println(dateTime.getMonth()); // JANUARY
int getMonthValue ()
final var dateTime = OffsetDateTime.of(2100, 1, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-15T00:00Z
System.out.println(dateTime.getMonthValue()); // 1
final var dateTime = OffsetDateTime.of(2100, 2, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-02-15T00:00Z
System.out.println(dateTime.getMonthValue()); // 2
final var dateTime = OffsetDateTime.of(2100, 3, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-03-15T00:00Z
System.out.println(dateTime.getMonthValue()); // 3
final var dateTime = OffsetDateTime.of(2100, 12, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-12-15T00:00Z
System.out.println(dateTime.getMonthValue()); // 12
final var dateTime = OffsetDateTime.of(2101, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2101-01-01T00:00Z
System.out.println(dateTime.getMonthValue()); // 1
int getNano ()
final var offset = ZoneOffset.UTC;
System.out.println(offset); // Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 0, offset);
System.out.println(dateTime); // 2100-01-01T13:30:15Z
System.out.println(dateTime.getNano()); // 0
final var offset = ZoneOffset.UTC;
System.out.println(offset); // Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 123456789, offset);
System.out.println(dateTime); // 2100-01-01T13:30:15.123456789Z
System.out.println(dateTime.getNano()); // 123456789
ZoneOffset getOffset ()
final var offset = ZoneOffset.UTC;
System.out.println(offset); // Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 0, offset);
System.out.println(dateTime); // 2100-01-01T13:30:15Z
System.out.println(dateTime.getOffset()); // Z
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00
final var dateTime = OffsetDateTime.of(1800, 12, 31, 1, 2, 3, 0, offset);
System.out.println(dateTime); // 1800-12-31T01:02:03+04:00
System.out.println(dateTime.getOffset()); // +04:00
int getSecond ()
final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:15Z
System.out.println(dateTime.getSecond()); // 15
int getYear ()
final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
System.out.println(dateTime.getYear()); // 2100
final var dateTime = OffsetDateTime.of(-3000, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // -3000-01-01T00:00Z
System.out.println(dateTime.getYear()); // -3000
final var dateTime = OffsetDateTime.of(9999, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 9999-01-01T00:00Z
System.out.println(dateTime.getYear()); // 9999
int hashCode ()
final var offset1 = ZoneOffset.ofHours(4);
final var dateTime1 = OffsetDateTime.of(2100, 1, 1, 1, 0, 0, 0, offset1);
System.out.println(dateTime1.hashCode()); // 821637959
final var offset2 = ZoneOffset.UTC;
final var dateTime2 = OffsetDateTime.of(1800, 12, 31, 23, 59, 59, 123, offset2);
System.out.println(dateTime2.hashCode()); // 1435253744
boolean isAfter (OffsetDateTime other)
final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
final var dateTime2 = OffsetDateTime.of(2099, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime3 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime4 = OffsetDateTime.of(2101, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime2); // 2099-01-01T00:00Z
System.out.println(dateTime3); // 2100-01-01T00:00Z
System.out.println(dateTime4); // 2101-01-01T00:00Z
System.out.println(dateTime.isAfter(dateTime2)); // true
System.out.println(dateTime.isAfter(dateTime3)); // false
System.out.println(dateTime.isAfter(dateTime4)); // false
final var dateTime = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(4));
System.out.println(dateTime); // 1800-01-01T00:00+04:00
final var dateTime2 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(3));
final var dateTime3 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(4));
final var dateTime4 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(5));
System.out.println(dateTime2); // 1800-01-01T00:00+03:00
System.out.println(dateTime3); // 1800-01-01T00:00+04:00
System.out.println(dateTime4); // 1800-01-01T00:00+05:00
System.out.println(dateTime.isAfter(dateTime2)); // false
System.out.println(dateTime.isAfter(dateTime3)); // false
System.out.println(dateTime.isAfter(dateTime4)); // true
boolean isBefore (OffsetDateTime other)
final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
final var dateTime2 = OffsetDateTime.of(2099, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime3 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime4 = OffsetDateTime.of(2101, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime2); // 2099-01-01T00:00Z
System.out.println(dateTime3); // 2100-01-01T00:00Z
System.out.println(dateTime4); // 2101-01-01T00:00Z
System.out.println(dateTime.isBefore(dateTime2)); // false
System.out.println(dateTime.isBefore(dateTime3)); // false
System.out.println(dateTime.isBefore(dateTime4)); // true
final var dateTime = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(4));
System.out.println(dateTime); // 1800-01-01T00:00+04:00
final var dateTime2 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(3));
final var dateTime3 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(4));
final var dateTime4 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(5));
System.out.println(dateTime2); // 1800-01-01T00:00+03:00
System.out.println(dateTime3); // 1800-01-01T00:00+04:00
System.out.println(dateTime4); // 1800-01-01T00:00+05:00
System.out.println(dateTime.isBefore(dateTime2)); // true
System.out.println(dateTime.isBefore(dateTime3)); // false
System.out.println(dateTime.isBefore(dateTime4)); // false
boolean isEqual (OffsetDateTime other)
final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
final var dateTime2 = OffsetDateTime.of(2099, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime3 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime4 = OffsetDateTime.of(2101, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime2); // 2099-01-01T00:00Z
System.out.println(dateTime3); // 2100-01-01T00:00Z
System.out.println(dateTime4); // 2101-01-01T00:00Z
System.out.println(dateTime.isEqual(dateTime2)); // false
System.out.println(dateTime.isEqual(dateTime3)); // true
System.out.println(dateTime.isEqual(dateTime4)); // false
final var dateTime = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(4));
System.out.println(dateTime); // 1800-01-01T00:00+04:00
final var dateTime2 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(3));
final var dateTime3 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(4));
final var dateTime4 = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(5));
System.out.println(dateTime2); // 1800-01-01T00:00+03:00
System.out.println(dateTime3); // 1800-01-01T00:00+04:00
System.out.println(dateTime4); // 1800-01-01T00:00+05:00
System.out.println(dateTime.isEqual(dateTime2)); // false
System.out.println(dateTime.isEqual(dateTime3)); // true
System.out.println(dateTime.isEqual(dateTime4)); // false
boolean isSupported (TemporalField field)
final var dateTime = OffsetDateTime.of(2100, 4, 13, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-13T12:30Z
System.out.println(dateTime.isSupported(ChronoField.YEAR)); // true
System.out.println(dateTime.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(dateTime.isSupported(ChronoField.DAY_OF_MONTH)); // true
System.out.println(dateTime.isSupported(ChronoField.HOUR_OF_DAY)); // true
System.out.println(dateTime.isSupported(ChronoField.MINUTE_OF_HOUR)); // true
System.out.println(dateTime.isSupported(ChronoField.SECOND_OF_MINUTE)); // true
System.out.println(dateTime.isSupported(ChronoField.NANO_OF_SECOND)); // true
System.out.println(dateTime.isSupported(ChronoField.OFFSET_SECONDS)); // true
boolean isSupported (TemporalUnit unit)
final var dateTime = OffsetDateTime.of(2100, 4, 13, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-13T12:30Z
System.out.println(dateTime.isSupported(ChronoUnit.YEARS)); // true
System.out.println(dateTime.isSupported(ChronoUnit.MONTHS)); // true
System.out.println(dateTime.isSupported(ChronoUnit.WEEKS)); // true
System.out.println(dateTime.isSupported(ChronoUnit.DAYS)); // true
System.out.println(dateTime.isSupported(ChronoUnit.HOURS)); // true
System.out.println(dateTime.isSupported(ChronoUnit.MINUTES)); // true
System.out.println(dateTime.isSupported(ChronoUnit.SECONDS)); // true
System.out.println(dateTime.isSupported(ChronoUnit.NANOS)); // true
System.out.println(dateTime.isSupported(ChronoUnit.FOREVER)); // false
OffsetDateTime minus (long amountToSubtract, TemporalUnit unit)
final var dateTime = OffsetDateTime.of(2100, 4, 13, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-13T00:00Z
final var ret1 = dateTime.minus(1, ChronoUnit.YEARS);
System.out.println(ret1); // 2099-04-13T00:00Z
final var ret2 = dateTime.minus(2, ChronoUnit.MONTHS);
System.out.println(ret2); // 2100-02-13T00:00Z
final var ret3 = dateTime.minus(3, ChronoUnit.DAYS);
System.out.println(ret3); // 2100-04-10T00:00Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 10, 20, 30, 123, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T10:20:30.000000123Z
final var ret1 = dateTime.minus(1, ChronoUnit.HOURS);
System.out.println(ret1); // 2100-01-01T09:20:30.000000123Z
final var ret2 = dateTime.minus(2, ChronoUnit.MINUTES);
System.out.println(ret2); // 2100-01-01T10:18:30.000000123Z
final var ret3 = dateTime.minus(3, ChronoUnit.SECONDS);
System.out.println(ret3); // 2100-01-01T10:20:27.000000123Z
final var ret4 = dateTime.minus(4, ChronoUnit.NANOS);
System.out.println(ret4); // 2100-01-01T10:20:30.000000119Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
final var ret1 = dateTime.minus(1, ChronoUnit.HOURS);
System.out.println(ret1); // 2099-12-31T23:00Z
final var ret2 = dateTime.minus(365, ChronoUnit.DAYS);
System.out.println(ret2); // 2099-01-01T00:00Z
final var ret3 = dateTime.minus(3000, ChronoUnit.YEARS);
System.out.println(ret3); // -0900-01-01T00:00Z
OffsetDateTime minus (TemporalAmount amountToSubtract)
final var dateTime = OffsetDateTime.of(2100, 4, 13, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-13T00:00Z
final var ret1 = dateTime.minus(Period.ofYears(1));
System.out.println(ret1); // 2099-04-13T00:00Z
final var ret2 = dateTime.minus(Period.ofMonths(2));
System.out.println(ret2); // 2100-02-13T00:00Z
final var ret3 = dateTime.minus(Period.ofDays(3));
System.out.println(ret3); // 2100-04-10T00:00Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 10, 20, 30, 123, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T10:20:30.000000123Z
final var ret1 = dateTime.minus(Duration.ofHours(1));
System.out.println(ret1); // 2100-01-01T09:20:30.000000123Z
final var ret2 = dateTime.minus(Duration.ofMinutes(2));
System.out.println(ret2); // 2100-01-01T10:18:30.000000123Z
final var ret3 = dateTime.minus(Duration.ofSeconds(3));
System.out.println(ret3); // 2100-01-01T10:20:27.000000123Z
final var ret4 = dateTime.minus(Duration.ofNanos(4));
System.out.println(ret4); // 2100-01-01T10:20:30.000000119Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
final var ret1 = dateTime.minus(Duration.ofHours(1));
System.out.println(ret1); // 2099-12-31T23:00Z
final var ret2 = dateTime.minus(Period.ofDays(365));
System.out.println(ret2); // 2099-01-01T00:00Z
final var ret3 = dateTime.minus(Period.ofYears(3000));
System.out.println(ret3); // -0900-01-01T00:00Z
OffsetDateTime minusDays (long days)
final var dateTime = OffsetDateTime.of(2100, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-05-20T00:00Z
System.out.println(dateTime.minusDays(1)); // 2100-05-19T00:00Z
System.out.println(dateTime.minusDays(2)); // 2100-05-18T00:00Z
System.out.println(dateTime.minusDays(20)); // 2100-04-30T00:00Z
System.out.println(dateTime.minusDays(365)); // 2099-05-20T00:00Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
System.out.println(dateTime.minusDays(1)); // 2099-12-31T00:00Z
OffsetDateTime minusHours (long hours)
final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:15Z
System.out.println(dateTime.minusHours(1)); // 2100-01-01T12:30:15Z
System.out.println(dateTime.minusHours(2)); // 2100-01-01T11:30:15Z
System.out.println(dateTime.minusHours(13)); // 2100-01-01T00:30:15Z
System.out.println(dateTime.minusHours(14)); // 2099-12-31T23:30:15Z
OffsetDateTime minusMinutes (long minutes)
final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30Z
System.out.println(dateTime.minusMinutes(1)); // 2100-01-01T13:29Z
System.out.println(dateTime.minusMinutes(2)); // 2100-01-01T13:28Z
System.out.println(dateTime.minusMinutes(30)); // 2100-01-01T13:00Z
System.out.println(dateTime.minusMinutes(31)); // 2100-01-01T12:59Z
System.out.println(dateTime.minusMinutes(60 * 14)); // 2099-12-31T23:30Z
OffsetDateTime minusMonths (long months)
final var dateTime = OffsetDateTime.of(2100, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-05-20T00:00Z
System.out.println(dateTime.minusMonths(1)); // 2100-04-20T00:00Z
System.out.println(dateTime.minusMonths(2)); // 2100-03-20T00:00Z
System.out.println(dateTime.minusMonths(12)); // 2099-05-20T00:00Z
final var dateTime = OffsetDateTime.of(2101, 3, 31, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2101-03-31T00:00Z
System.out.println(dateTime.minusMonths(1)); // 2101-02-28T00:00Z
OffsetDateTime minusNanos (long nanos)
final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:15.000000123Z
System.out.println(dateTime.minusNanos(1)); // 2100-01-01T13:30:15.000000122Z
System.out.println(dateTime.minusNanos(2)); // 2100-01-01T13:30:15.000000121Z
System.out.println(dateTime.minusNanos(123)); // 2100-01-01T13:30:15Z
System.out.println(dateTime.minusNanos(124)); // 2100-01-01T13:30:14.999999999Z
OffsetDateTime minusSeconds (long seconds)
final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:15Z
System.out.println(dateTime.minusSeconds(1)); // 2100-01-01T13:30:14Z
System.out.println(dateTime.minusSeconds(2)); // 2100-01-01T13:30:13Z
System.out.println(dateTime.minusSeconds(15)); // 2100-01-01T13:30Z
System.out.println(dateTime.minusSeconds(16)); // 2100-01-01T13:29:59Z
System.out.println(dateTime.minusSeconds(60 * 60)); // 2100-01-01T12:30:15Z
System.out.println(dateTime.minusSeconds(60 * 60 * 24)); // 2099-12-31T13:30:15Z
OffsetDateTime minusWeeks (long weeks)
final var dateTime = OffsetDateTime.of(2100, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-05-20T00:00Z
System.out.println(dateTime.minusWeeks(1)); //2100-05-13T00:00Z
System.out.println(dateTime.minusWeeks(2)); // 2100-05-06T00:00Z
System.out.println(dateTime.minusWeeks(20)); // 2099-12-31T00:00Z
System.out.println(dateTime.minusWeeks(100)); // 2098-06-19T00:00Z
OffsetDateTime minusYears (long years)
final var dateTime = OffsetDateTime.of(2100, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-05-20T00:00Z
System.out.println(dateTime.minusYears(1)); // 2099-05-20T00:00Z
System.out.println(dateTime.minusYears(2)); // 2098-05-20T00:00Z
System.out.println(dateTime.minusYears(10)); // 2090-05-20T00:00Z
final var dateTime = OffsetDateTime.of(2104, 2, 29, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2104-02-29T00:00Z
System.out.println(dateTime.minusYears(1)); // 2103-02-28T00:00Z
static OffsetDateTime now ()
final var zone = ZoneId.systemDefault();
System.out.println(zone); // Asia/Tokyo
System.out.println(OffsetDateTime.now()); // 2024-07-16T16:08:47.230876400+09:00
static OffsetDateTime now (Clock clock)
// 意図的に5日進めたClock
final var clock = Clock.offset(Clock.systemDefaultZone(), Duration.ofDays(5));
final var zone = ZoneId.systemDefault();
System.out.println(zone); // Asia/Tokyo
final var dateTime1 = OffsetDateTime.now();
System.out.println(dateTime1); // 2024-07-16T16:09:15.706065400+09:00
final var dateTime2 = OffsetDateTime.now(clock);
System.out.println(dateTime2); // 2024-07-21T16:09:15.706065400+09:00
static OffsetDateTime now (ZoneId zone)
final var zone1 = ZoneOffset.UTC;
System.out.println(zone1); // Z
final var zone2 = ZoneId.of("Etc/GMT+1");
System.out.println(zone2); // Etc/GMT+1
final var dateTime1 = OffsetDateTime.now(zone1);
System.out.println(dateTime1); // 2024-07-16T07:13:17.397503100Z
final var dateTime2 = OffsetDateTime.now(zone2);
System.out.println(dateTime2); // 2024-07-16T06:13:17.397503100-01:00
static OffsetDateTime of (int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset)
final var ret1 = OffsetDateTime.of(2100, 1, 2, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(ret1); // 2100-01-02T12:30Z
final var ret2 = OffsetDateTime.of(1800, 12, 31, 1, 2, 3, 456, ZoneOffset.ofHours(4));
System.out.println(ret2); // 1800-12-31T01:02:03.000000456+04:00
static OffsetDateTime of (LocalDate date, LocalTime time, ZoneOffset offset)
final var date = LocalDate.of(2100, 1, 2);
System.out.println(date); // 2100-01-02
final var time = LocalTime.of(12, 30);
System.out.println(time); // 12:30
final var ret = OffsetDateTime.of(date, time, ZoneOffset.UTC);
System.out.println(ret); // 2100-01-02T12:30Z
final var date = LocalDate.of(1800, 12, 31);
System.out.println(date); // 1800-12-31
final var time = LocalTime.of(1, 2, 3, 456);
System.out.println(time); // 01:02:03.000000456
final var ret = OffsetDateTime.of(date, time, ZoneOffset.ofHours(4));
System.out.println(ret); // 1800-12-31T01:02:03.000000456+04:00
static OffsetDateTime of (LocalDateTime dateTime, ZoneOffset offset)
final var dateTime = LocalDateTime.of(2100, 1, 2, 12, 30);
System.out.println(dateTime); // 2100-01-02T12:30
final var ret = OffsetDateTime.of(dateTime, ZoneOffset.UTC);
System.out.println(ret); // 2100-01-02T12:30Z
final var dateTime = LocalDateTime.of(1800, 12, 31, 1, 2, 3, 456);
System.out.println(dateTime); // 1800-12-31T01:02:03.000000456
final var ret = OffsetDateTime.of(dateTime, ZoneOffset.ofHours(4));
System.out.println(ret); // 1800-12-31T01:02:03.000000456+04:00
static OffsetDateTime ofInstant (Instant instant, ZoneId zone)
final var instant = Instant.ofEpochSecond(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z
final var dateTime1 = OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-01-01T00:00Z
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var dateTime2 = OffsetDateTime.ofInstant(instant, zone);
System.out.println(dateTime2); // 2099-12-31T23:00-01:00
static OffsetDateTime parse (CharSequence text)
final var ret1 = OffsetDateTime.parse("2100-04-14T00:00Z");
System.out.println(ret1); // 2100-04-14T00:00Z
final var ret2 = OffsetDateTime.parse("2100-04-14T13:30:45Z");
System.out.println(ret2); // 2100-04-14T13:30:45Z
final var ret3 = OffsetDateTime.parse("2100-04-14T13:30:45.123456789Z");
System.out.println(ret3); // 2100-04-14T13:30:45.123456789Z
final var ret4 = OffsetDateTime.parse("1800-01-01T01:02:03+09:00");
System.out.println(ret4); // 1800-01-01T01:02:03+09:00
try {
final var ret = OffsetDateTime.parse("2100-04-14T00:00");
} catch (DateTimeParseException e) {
System.out.println("DateTimeParseException! : " + e.getMessage());
}
// 結果
// ↓
//DateTimeParseException! : Text '2100-04-14T00:00' could not be parsed at index 16
static OffsetDateTime parse (CharSequence text, DateTimeFormatter formatter)
final var ret1 = OffsetDateTime.parse(
"2100-04-14T12:30:45Z", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
System.out.println(ret1); // 2100-04-14T12:30:45Z
final var ret2 = OffsetDateTime.parse(
"1800/1/2 3:4:5 -0400", DateTimeFormatter.ofPattern("y/M/d H:m:s X"));
System.out.println(ret2); // 1800-01-02T03:04:05-04:00
final var ret3 = OffsetDateTime.parse(
"2100年4月14日12時30分45秒 GMT+9", DateTimeFormatter.ofPattern("y年M月d日H時m分s秒 O"));
System.out.println(ret3); // 2100-04-14T12:30:45+09:00
OffsetDateTime plus (long amountToAdd, TemporalUnit unit)
final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
final var ret1 = dateTime.plus(1, ChronoUnit.YEARS);
System.out.println(ret1); // 2101-01-01T00:00Z
final var ret2 = dateTime.plus(2, ChronoUnit.MONTHS);
System.out.println(ret2); // 2100-03-01T00:00Z
final var ret3 = dateTime.plus(3, ChronoUnit.DAYS);
System.out.println(ret3); // 2100-01-04T00:00Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 10, 20, 30, 123, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T10:20:30.000000123Z
final var ret1 = dateTime.plus(1, ChronoUnit.HOURS);
System.out.println(ret1); // 2100-01-01T11:20:30.000000123Z
final var ret2 = dateTime.plus(2, ChronoUnit.MINUTES);
System.out.println(ret2); // 2100-01-01T10:22:30.000000123Z
final var ret3 = dateTime.plus(3, ChronoUnit.SECONDS);
System.out.println(ret3); // 2100-01-01T10:20:33.000000123Z
final var ret4 = dateTime.plus(4, ChronoUnit.NANOS);
System.out.println(ret4); // 2100-01-01T10:20:30.000000127Z
final var dateTime = OffsetDateTime.of(2099, 12, 31, 23, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2099-12-31T23:00Z
final var ret1 = dateTime.plus(1, ChronoUnit.HOURS);
System.out.println(ret1); // 2100-01-01T00:00Z
final var ret2 = dateTime.plus(366, ChronoUnit.DAYS);
System.out.println(ret2); // 2101-01-01T23:00Z
final var ret3 = dateTime.plus(3000, ChronoUnit.YEARS);
System.out.println(ret3); // 5099-12-31T23:00Z
OffsetDateTime plus (TemporalAmount amountToAdd)
final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
final var ret1 = dateTime.plus(Period.ofYears(1));
System.out.println(ret1); // 2101-01-01T00:00Z
final var ret2 = dateTime.plus(Period.ofMonths(2));
System.out.println(ret2); // 2100-03-01T00:00Z
final var ret3 = dateTime.plus(Period.ofDays(3));
System.out.println(ret3); // 2100-01-04T00:00Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 10, 20, 30, 123, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T10:20:30.000000123Z
final var ret1 = dateTime.plus(Duration.ofHours(1));
System.out.println(ret1); // 2100-01-01T11:20:30.000000123Z
final var ret2 = dateTime.plus(Duration.ofMinutes(2));
System.out.println(ret2); // 2100-01-01T10:22:30.000000123Z
final var ret3 = dateTime.plus(Duration.ofSeconds(3));
System.out.println(ret3); // 2100-01-01T10:20:33.000000123Z
final var ret4 = dateTime.plus(Duration.ofNanos(4));
System.out.println(ret4); // 2100-01-01T10:20:30.000000127Z
final var dateTime = OffsetDateTime.of(2099, 12, 31, 23, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2099-12-31T23:00Z
final var ret1 = dateTime.plus(Duration.ofHours(1));
System.out.println(ret1); // 2100-01-01T00:00Z
final var ret2 = dateTime.plus(Period.ofDays(366));
System.out.println(ret2); // 2101-01-01T23:00Z
final var ret3 = dateTime.plus(Period.ofYears(3000));
System.out.println(ret3); // 5099-12-31T23:00Z
OffsetDateTime plusDays (long days)
final var dateTime = OffsetDateTime.of(2100, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-05-20T00:00Z
System.out.println(dateTime.plusDays(1)); // 2100-05-21T00:00Z
System.out.println(dateTime.plusDays(2)); // 2100-05-22T00:00Z
System.out.println(dateTime.plusDays(12)); // 2100-06-01T00:00Z
System.out.println(dateTime.plusDays(365)); // 2101-05-20T00:00Z
final var dateTime = OffsetDateTime.of(2100, 12, 31, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-12-31T00:00Z
System.out.println(dateTime.plusDays(1)); // 2101-01-01T00:00Z
OffsetDateTime plusHours (long hours)
final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:00Z
System.out.println(dateTime.plusHours(1)); // 2100-01-01T14:00Z
System.out.println(dateTime.plusHours(2)); // 2100-01-01T15:00Z
System.out.println(dateTime.plusHours(11)); // 2100-01-02T00:00Z
System.out.println(dateTime.plusHours(12)); // 2100-01-02T01:00Z
OffsetDateTime plusMinutes (long minutes)
final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30Z
System.out.println(dateTime.plusMinutes(1)); // 2100-01-01T13:31Z
System.out.println(dateTime.plusMinutes(2)); // 2100-01-01T13:32Z
System.out.println(dateTime.plusMinutes(30)); // 2100-01-01T14:00Z
System.out.println(dateTime.plusMinutes(31)); // 2100-01-01T14:01Z
System.out.println(dateTime.plusMinutes(60 * 24)); // 2100-01-02T13:30Z
OffsetDateTime plusMonths (long months)
final var dateTime = OffsetDateTime.of(2100, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-05-20T00:00Z
System.out.println(dateTime.plusMonths(1)); // 2100-06-20T00:00Z
System.out.println(dateTime.plusMonths(2)); // 2100-07-20T00:00Z
System.out.println(dateTime.plusMonths(12)); // 2101-05-20T00:00Z
final var dateTime = OffsetDateTime.of(2100, 3, 31, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-03-31T00:00Z
System.out.println(dateTime.plusMonths(1)); // 2100-04-30T00:00Z
OffsetDateTime plusNanos (long nanos)
final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:15.000000123Z
final var ret1 = dateTime.plusNanos(1);
System.out.println(ret1); // 2100-01-01T13:30:15.000000124Z
final var ret2 = dateTime.plusNanos(2);
System.out.println(ret2); // 2100-01-01T13:30:15.000000125Z
final var ret3 = dateTime.plusNanos(1000000000L - 124);
System.out.println(ret3); // 2100-01-01T13:30:15.999999999Z
final var ret4 = dateTime.plusNanos(1000000000L - 123);
System.out.println(ret4); // 2100-01-01T13:30:16Z
OffsetDateTime plusSeconds (long seconds)
final var dateTime = OffsetDateTime.of(2100, 1, 1, 13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:15Z
System.out.println(dateTime.plusSeconds(1)); // 2100-01-01T13:30:16Z
System.out.println(dateTime.plusSeconds(2)); // 2100-01-01T13:30:17Z
System.out.println(dateTime.plusSeconds(45)); // 2100-01-01T13:31Z
System.out.println(dateTime.plusSeconds(46)); // 2100-01-01T13:31:01Z
System.out.println(dateTime.plusSeconds(60 + 45)); // 2100-01-01T13:32Z
System.out.println(dateTime.plusSeconds(60 * 60)); // 2100-01-01T14:30:15Z
System.out.println(dateTime.plusSeconds(60 * 60 * 24)); // 2100-01-02T13:30:15Z
OffsetDateTime plusWeeks (long weeks)
final var dateTime = OffsetDateTime.of(2100, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-05-20T00:00Z
System.out.println(dateTime.plusWeeks(1)); // 2100-05-27T00:00Z
System.out.println(dateTime.plusWeeks(2)); // 2100-06-03T00:00Z
System.out.println(dateTime.plusWeeks(20)); // 2100-10-07T00:00Z
System.out.println(dateTime.plusWeeks(100)); // 2102-04-20T00:00Z
OffsetDateTime plusYears (long years)
final var dateTime = OffsetDateTime.of(2100, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-05-20T00:00Z
System.out.println(dateTime.plusYears(1)); // 2101-05-20T00:00Z
System.out.println(dateTime.plusYears(2)); // 2102-05-20T00:00Z
System.out.println(dateTime.plusYears(10)); // 2110-05-20T00:00Z
final var dateTime = OffsetDateTime.of(2104, 2, 29, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2104-02-29T00:00Z
System.out.println(dateTime.plusYears(1)); // 2105-02-28T00:00Z
<R> R query (TemporalQuery<R> query)
final var dateTime = OffsetDateTime.of(2100, 4, 14, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T12:30Z
System.out.println(dateTime.query(TemporalQueries.localDate())); // 2100-04-14
System.out.println(dateTime.query(TemporalQueries.localTime())); // 12:30
System.out.println(dateTime.query(TemporalQueries.offset())); // Z
System.out.println(dateTime.query(TemporalQueries.precision())); // Nanos
ValueRange range (TemporalField field)
final var dateTime = OffsetDateTime.of(2100, 4, 14, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T00:00Z
System.out.println(dateTime.range(ChronoField.YEAR)); // -999999999 - 999999999
System.out.println(dateTime.range(ChronoField.MONTH_OF_YEAR)); // 1 - 12
System.out.println(dateTime.range(ChronoField.DAY_OF_MONTH)); // 1 - 30
System.out.println(dateTime.range(ChronoField.HOUR_OF_DAY)); // 0 - 23
System.out.println(dateTime.range(ChronoField.MINUTE_OF_HOUR)); // 0 - 59
System.out.println(dateTime.range(ChronoField.SECOND_OF_MINUTE)); // 0 - 59
System.out.println(dateTime.range(ChronoField.NANO_OF_SECOND)); // 0 - 999999999
System.out.println(dateTime.range(ChronoField.OFFSET_SECONDS)); // -64800 - 64800
final var dateTime1 = OffsetDateTime.of(2103, 2, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime1); // 2103-02-01T00:00Z
System.out.println(dateTime1.toLocalDate().isLeapYear()); // false
System.out.println(dateTime1.range(ChronoField.DAY_OF_MONTH)); // 1 - 28
final var dateTime2 = OffsetDateTime.of(2104, 2, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime2); // 2104-02-01T00:00Z
System.out.println(dateTime2.toLocalDate().isLeapYear()); // true
System.out.println(dateTime2.range(ChronoField.DAY_OF_MONTH)); // 1 - 29
static Comparator<OffsetDateTime> timeLineOrder ()
final var dateTime = OffsetDateTime.of(2100, 12, 3, 11, 0, 0, 0, ZoneOffset.ofHours(1));
System.out.println(dateTime); // 2100-12-03T11:00+01:00
final var dateTime1 = OffsetDateTime.of(2100, 12, 3, 10, 30, 0, 0, ZoneOffset.ofHours(1));
final var dateTime2 = OffsetDateTime.of(2100, 12, 3, 12, 0, 0, 0, ZoneOffset.ofHours(2));
final var dateTime3 = OffsetDateTime.of(2100, 12, 3, 11, 30, 0, 0, ZoneOffset.ofHours(1));
System.out.println(dateTime1); // 2100-12-03T10:30+01:00
System.out.println(dateTime2); // 2100-12-03T12:00+02:00
System.out.println(dateTime3); // 2100-12-03T11:30+01:00
final var comparator = OffsetDateTime.timeLineOrder();
System.out.println(comparator.compare(dateTime, dateTime1)); // 1
System.out.println(comparator.compare(dateTime, dateTime2)); // 0
System.out.println(comparator.compare(dateTime, dateTime3)); // -1
System.out.println(dateTime.compareTo(dateTime1)); // 1
System.out.println(dateTime.compareTo(dateTime2)); // -1
System.out.println(dateTime.compareTo(dateTime3)); // -1
long toEpochSecond ()
final var dateTime = OffsetDateTime.of(2100, 1, 2, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-02T00:00Z
final long ret = dateTime.toEpochSecond();
System.out.println(ret); // 4102531200
System.out.println(Instant.ofEpochSecond(ret)); // 2100-01-02T00:00:00Z
final var offset = ZoneOffset.ofHours(8);
System.out.println(offset); // +08:00
final var dateTime = OffsetDateTime.of(1800, 12, 31, 12, 30, 0, 0, offset);
System.out.println(dateTime); // 1800-12-31T12:30+08:00
final long ret = dateTime.toEpochSecond();
System.out.println(ret); // -5333196600
System.out.println(Instant.ofEpochSecond(ret)); // 1800-12-31T04:30:00Z
Instant toInstant ()
final var dateTime = OffsetDateTime.of(2100, 1, 2, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-02T00:00Z
final var ret = dateTime.toInstant();
System.out.println(ret); // 2100-01-02T00:00:00Z
final var offset = ZoneOffset.ofHours(8);
System.out.println(offset); // +08:00
final var dateTime = OffsetDateTime.of(1800, 12, 31, 12, 30, 0, 0, offset);
System.out.println(dateTime); // 1800-12-31T12:30+08:00
final var ret = dateTime.toInstant();
System.out.println(ret); // 1800-12-31T04:30:00Z
LocalDate toLocalDate ()
final var dateTime = OffsetDateTime.of(2100, 4, 14, 12, 30, 45, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T12:30:45Z
System.out.println(dateTime.toLocalDate()); // 2100-04-14
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00
final var dateTime = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, offset);
System.out.println(dateTime); // 1800-01-01T00:00+04:00
System.out.println(dateTime.toLocalDate()); // 1800-01-01
LocalDateTime toLocalDateTime ()
final var dateTime = OffsetDateTime.of(2100, 4, 14, 12, 30, 45, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T12:30:45Z
System.out.println(dateTime.toLocalDateTime()); // 2100-04-14T12:30:45
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00
final var dateTime = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, offset);
System.out.println(dateTime); // 1800-01-01T00:00+04:00
System.out.println(dateTime.toLocalDateTime()); // 1800-01-01T00:00
LocalTime toLocalTime ()
final var dateTime = OffsetDateTime.of(2100, 4, 14, 12, 30, 45, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T12:30:45Z
System.out.println(dateTime.toLocalTime()); // 12:30:45
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00
final var dateTime = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, offset);
System.out.println(dateTime); // 1800-01-01T00:00+04:00
System.out.println(dateTime.toLocalTime()); // 00:00
OffsetTime toOffsetTime ()
final var dateTime = OffsetDateTime.of(2100, 4, 14, 12, 30, 45, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T12:30:45Z
System.out.println(dateTime.toOffsetTime()); // 12:30:45Z
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00
final var dateTime = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, offset);
System.out.println(dateTime); // 1800-01-01T00:00+04:00
System.out.println(dateTime.toOffsetTime()); // 00:00+04:00
String toString ()
final var offset1 = ZoneOffset.UTC;
final var str1 = OffsetDateTime.of(2100, 1, 2, 0, 0, 0, 0, offset1).toString();
System.out.println(str1); // 2100-01-02T00:00Z
final var offset2 = ZoneOffset.ofHours(4);
final var str2 = OffsetDateTime.of(1800, 12, 31, 1, 2, 3, 456, offset2).toString();
System.out.println(str2); // 1800-12-31T01:02:03.000000456+04:00
ZonedDateTime toZonedDateTime ()
final var dateTime = OffsetDateTime.of(2100, 4, 14, 12, 30, 45, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T12:30:45Z
System.out.println(dateTime.toZonedDateTime()); // 2100-04-14T12:30:45Z
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00
final var dateTime = OffsetDateTime.of(1800, 1, 1, 0, 0, 0, 0, offset);
System.out.println(dateTime); // 1800-01-01T00:00+04:00
System.out.println(dateTime.toZonedDateTime()); // 1800-01-01T00:00+04:00
OffsetDateTime truncatedTo (TemporalUnit unit)
final var offset = ZoneOffset.UTC;
System.out.println(offset); // Z
final var dateTime = OffsetDateTime.of(2100, 4, 14, 12, 30, 15, 123456789, offset);
System.out.println(dateTime); // 2100-04-14T12:30:15.123456789Z
System.out.println(dateTime.truncatedTo(ChronoUnit.DAYS)); // 2100-04-14T00:00Z
System.out.println(dateTime.truncatedTo(ChronoUnit.HOURS)); // 2100-04-14T12:00Z
System.out.println(dateTime.truncatedTo(ChronoUnit.MINUTES)); // 2100-04-14T12:30Z
System.out.println(dateTime.truncatedTo(ChronoUnit.SECONDS)); // 2100-04-14T12:30:15Z
System.out.println(dateTime.truncatedTo(ChronoUnit.NANOS)); // 2100-04-14T12:30:15.123456789Z
long until (Temporal endExclusive, TemporalUnit unit)
final var dateTime1 = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2101, 2, 2, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-01-01T00:00Z
System.out.println(dateTime2); // 2101-02-02T00:00Z
System.out.println(dateTime1.until(dateTime2, ChronoUnit.YEARS)); // 1
System.out.println(dateTime1.until(dateTime2, ChronoUnit.MONTHS)); // 13
System.out.println(dateTime1.until(dateTime2, ChronoUnit.DAYS)); // 397
System.out.println(dateTime1.until(dateTime2, ChronoUnit.HOURS)); // 9528
System.out.println(dateTime1.until(dateTime2, ChronoUnit.MINUTES)); // 571680
System.out.println(dateTime1.until(dateTime2, ChronoUnit.SECONDS)); // 34300800
System.out.println(dateTime1.until(dateTime2, ChronoUnit.NANOS)); // 34300800000000000
final var dateTime1 = OffsetDateTime.of(2100, 8, 14, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime2 = OffsetDateTime.of(2100, 6, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-08-14T00:00Z
System.out.println(dateTime2); // 2100-06-15T00:00Z
System.out.println(dateTime1.until(dateTime2, ChronoUnit.YEARS)); // 0
System.out.println(dateTime1.until(dateTime2, ChronoUnit.MONTHS)); // -1
System.out.println(dateTime1.until(dateTime2, ChronoUnit.DAYS)); // -60
System.out.println(dateTime1.until(dateTime2, ChronoUnit.HOURS)); // -1440
System.out.println(dateTime1.until(dateTime2, ChronoUnit.MINUTES)); // -86400
System.out.println(dateTime1.until(dateTime2, ChronoUnit.SECONDS)); // -5184000
System.out.println(dateTime1.until(dateTime2, ChronoUnit.NANOS)); // -5184000000000000
final var offset1 = ZoneOffset.ofHours(3);
final var offset2 = ZoneOffset.ofHours(-5);
final var dateTime1 = OffsetDateTime.of(1999, 1, 1, 1, 0, 0, 0, offset1);
final var dateTime2 = OffsetDateTime.of(1999, 1, 1, 1, 0, 0, 0, offset2);
System.out.println(dateTime1); // 1999-01-01T01:01:01+03:00
System.out.println(dateTime2); // 1999-01-01T01:01:01-05:00
System.out.println(dateTime1.until(dateTime2, ChronoUnit.HOURS)); // 8
System.out.println(dateTime1.until(dateTime2, ChronoUnit.MINUTES)); // 480
System.out.println(dateTime1.until(dateTime2, ChronoUnit.SECONDS)); // 28800
System.out.println(dateTime1.until(dateTime2, ChronoUnit.NANOS)); // 28800000000000
OffsetDateTime with (TemporalAdjuster adjuster)
final var dateTime = OffsetDateTime.of(2100, 1, 15, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-15T00:00Z
final var ret1 = dateTime.with(TemporalAdjusters.firstDayOfMonth());
System.out.println(ret1); // 2100-01-01T00:00Z
final var ret2 = dateTime.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(ret2); // 2100-01-31T00:00Z
final var dateTime = OffsetDateTime.of(2100, 4, 14, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T12:30Z
System.out.println(dateTime.with(Year.of(1800))); // 1800-04-14T12:30Z
System.out.println(dateTime.with(Month.JANUARY)); // 2100-01-14T12:30Z
System.out.println(dateTime.with(LocalTime.MAX)); // 2100-04-14T23:59:59.999999999Z
System.out.println(dateTime.with(LocalDate.MIN)); // -999999999-01-01T12:30Z
OffsetDateTime with (TemporalField field, long newValue)
final var dateTime = OffsetDateTime.of(2100, 1, 2, 12, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-02T12:00Z
final var ret1 = dateTime.with(ChronoField.YEAR, 1800);
System.out.println(ret1); // 1800-01-02T12:00Z
final var ret2 = dateTime.with(ChronoField.MONTH_OF_YEAR, 12);
System.out.println(ret2); // 2100-12-02T12:00Z
final var ret3 = dateTime.with(ChronoField.DAY_OF_MONTH, 30);
System.out.println(ret3); // 2100-01-30T12:00Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 12, 30, 45, 123000000, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T12:30:45.123Z
final var ret1 = dateTime.with(ChronoField.HOUR_OF_DAY, 23);
System.out.println(ret1); // 2100-01-01T23:30:45.123Z
final var ret2 = dateTime.with(ChronoField.MINUTE_OF_HOUR, 59);
System.out.println(ret2); // 2100-01-01T12:59:45.123Z
final var ret3 = dateTime.with(ChronoField.SECOND_OF_MINUTE, 0);
System.out.println(ret3); // 2100-01-01T12:30:00.123Z
final var ret4 = dateTime.with(ChronoField.NANO_OF_SECOND, 456);
System.out.println(ret4); // 2100-01-01T12:30:45.000000456Z
OffsetDateTime withDayOfMonth (int dayOfMonth)
final var dateTime = OffsetDateTime.of(2100, 4, 8, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-08T00:00Z
System.out.println(dateTime.withDayOfMonth(1)); // 2100-04-01T00:00Z
System.out.println(dateTime.withDayOfMonth(2)); // 2100-04-02T00:00Z
System.out.println(dateTime.withDayOfMonth(29)); // 2100-04-29T00:00Z
System.out.println(dateTime.withDayOfMonth(30)); // 2100-04-30T00:00Z
OffsetDateTime withDayOfYear (int dayOfYear)
final var dateTime = OffsetDateTime.of(2100, 4, 8, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-08T12:30Z
System.out.println(dateTime.withDayOfYear(1)); // 2100-01-01T12:30Z
System.out.println(dateTime.withDayOfYear(2)); // 2100-01-02T12:30Z
System.out.println(dateTime.withDayOfYear(30)); // 2100-01-30T12:30Z
System.out.println(dateTime.withDayOfYear(31)); // 2100-01-31T12:30Z
System.out.println(dateTime.withDayOfYear(32)); // 2100-02-01T12:30Z
System.out.println(dateTime.withDayOfYear(33)); // 2100-02-02T12:30Z
System.out.println(dateTime.withDayOfYear(365)); // 2100-12-31T12:30Z
OffsetDateTime withHour (int hour)
final var dateTime = OffsetDateTime.of(2100, 1, 1, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T12:30Z
System.out.println(dateTime.withHour(0)); // 2100-01-01T00:30Z
System.out.println(dateTime.withHour(23)); // 2100-01-01T23:30Z
OffsetDateTime withMinute (int minute)
final var dateTime = OffsetDateTime.of(2100, 1, 1, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T12:30Z
System.out.println(dateTime.withMinute(0)); // 2100-01-01T12:00Z
System.out.println(dateTime.withMinute(59)); // 2100-01-01T12:59Z
OffsetDateTime withMonth (int month)
final var dateTime = OffsetDateTime.of(2100, 4, 8, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-08T12:30Z
System.out.println(dateTime.withMonth(1)); // 2100-01-08T12:30Z
System.out.println(dateTime.withMonth(2)); // 2100-02-08T12:30Z
System.out.println(dateTime.withMonth(11)); // 2100-11-08T12:30Z
System.out.println(dateTime.withMonth(12)); // 2100-12-08T12:30Z
OffsetDateTime withNano (int nanoOfSecond)
final var dateTime = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 123, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00:00.000000123Z
System.out.println(dateTime.withNano(0)); // 2100-01-01T00:00Z
System.out.println(dateTime.withNano(999999999)); // 2100-01-01T00:00:00.999999999Z
OffsetDateTime withOffsetSameInstant (ZoneOffset offset)
final var offset = ZoneOffset.UTC;
System.out.println(offset); // Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 12, 30, 0, 0, offset);
System.out.println(dateTime); // 2100-01-01T12:30Z
final var ret = dateTime.withOffsetSameInstant(ZoneOffset.ofHours(4));
System.out.println(ret); // 2100-01-01T16:30+04:00
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00
final var dateTime = OffsetDateTime.of(2100, 1, 1, 12, 30, 0, 0, offset);
System.out.println(dateTime); // 2100-01-01T12:30+04:00
final var ret = dateTime.withOffsetSameInstant(ZoneOffset.UTC);
System.out.println(ret); // 2100-01-01T08:30Z
OffsetDateTime withOffsetSameLocal (ZoneOffset offset)
final var offset = ZoneOffset.UTC;
System.out.println(offset); // Z
final var dateTime = OffsetDateTime.of(2100, 1, 1, 12, 30, 0, 0, offset);
System.out.println(dateTime); // 2100-01-01T12:30Z
final var ret = dateTime.withOffsetSameLocal(ZoneOffset.ofHours(4));
System.out.println(ret); // 2100-01-01T12:30+04:00
final var offset = ZoneOffset.ofHours(4);
System.out.println(offset); // +04:00
final var dateTime = OffsetDateTime.of(2100, 1, 1, 12, 30, 0, 0, offset);
System.out.println(dateTime); // 2100-01-01T12:30+04:00
final var ret = dateTime.withOffsetSameLocal(ZoneOffset.UTC);
System.out.println(ret); // 2100-01-01T12:30Z
OffsetDateTime withSecond (int second)
final var dateTime = OffsetDateTime.of(2100, 1, 1, 12, 30, 45, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T12:30:45Z
System.out.println(dateTime.withSecond(0)); // 2100-01-01T12:30Z
System.out.println(dateTime.withSecond(1)); // 2100-01-01T12:30:01Z
System.out.println(dateTime.withSecond(59)); // 2100-01-01T12:30:59Z
OffsetDateTime withYear (int year)
final var dateTime = OffsetDateTime.of(2100, 4, 8, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-08T12:30Z
System.out.println(dateTime.withYear(1800)); // 1800-04-08T12:30Z
System.out.println(dateTime.withYear(-999)); // -0999-04-08T12:30Z
関連記事
- 日付・時刻の基本
- 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 (月・日)
- OffsetTime (時刻・オフセットあり)
- Period (日付の量)
- Temporal
- TemporalAccessor
- TemporalAdjuster (日付・時刻の調整)
- TemporalAdjusters (日付・時刻の調整ユーティリティ)
- TimeZone (タイムゾーン)
- Year (年)
- YearMonth (年・月)
- ZonedDateTime (日時・タイムゾーンあり)
- ZoneId (タイムゾーンID)
- ZoneOffset (タイムゾーン・オフセット)