Java : LocalDateTime (日時・タイムゾーンなし) - API使用例
LocalDateTime (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
LocalDateTime は日時を表すクラスです。
タイムゾーン情報は持ちません。
日時は、
- 日付 (年と月と日)
- 時刻 (時と分と秒とナノ秒)
で構成されます。
インスタンスの生成には of メソッドを使います。
関連記事:日付・時刻の基本
final var dateTime = LocalDateTime.of(2100, 4, 14, 13, 30, 15, 123000000);
System.out.println(dateTime); // 2100-04-14T13:30:15.123
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
フィールド
static final LocalDateTime MAX
System.out.println(LocalDateTime.MAX); // +999999999-12-31T23:59:59.999999999
static final LocalDateTime MIN
System.out.println(LocalDateTime.MIN); // -999999999-01-01T00:00
メソッド
Temporal adjustInto (Temporal temporal)
final var zone = ZoneId.systemDefault();
System.out.println(zone); // Asia/Tokyo
final var temporal = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, zone);
System.out.println(temporal); // 2100-01-01T00:00+09:00[Asia/Tokyo]
final var dateTime = LocalDateTime.of(1900, 4, 15, 1, 2, 3, 0);
System.out.println(dateTime); // 1900-04-15T01:02:03
final var ret = dateTime.adjustInto(temporal);
System.out.println(ret); // 1900-04-15T01:02:03+09:00[Asia/Tokyo]
final var offset = ZoneOffset.ofHours(-8);
System.out.println(offset); // -08:00
final var temporal = OffsetDateTime.of(2100, 1, 2, 3, 4, 5, 678000000, offset);
System.out.println(temporal); // 2100-01-02T03:04:05.678-08:00
final var dateTime = LocalDateTime.of(2500, 4, 14, 0, 0);
System.out.println(dateTime); // 2500-04-14T00:00
final var ret = dateTime.adjustInto(temporal);
System.out.println(ret); // 2500-04-14T00:00-08:00
OffsetDateTime atOffset (ZoneOffset offset)
final var dateTime = LocalDateTime.of(2100, 4, 14, 15, 20);
System.out.println(dateTime); // 2100-04-14T15:20
final var ret1 = dateTime.atOffset(ZoneOffset.UTC);
System.out.println(ret1); // 2100-04-14T15:20Z
final var ret2 = dateTime.atOffset(ZoneOffset.ofHours(9));
System.out.println(ret2); // 2100-04-14T15:20+09:00
final var ret3 = dateTime.atOffset(ZoneOffset.ofHoursMinutes(-7, -30));
System.out.println(ret3); // 2100-04-14T15:20-07:30
ZonedDateTime atZone (ZoneId zone)
final var dateTime = LocalDateTime.of(2100, 4, 14, 15, 20);
System.out.println(dateTime); // 2100-04-14T15:20
final var ret1 = dateTime.atZone(ZoneId.systemDefault());
System.out.println(ret1); // 2100-04-14T15:20+09:00[Asia/Tokyo]
final var ret2 = dateTime.atZone(ZoneId.of("America/Los_Angeles"));
System.out.println(ret2); // 2100-04-14T15:20-07:00[America/Los_Angeles]
final var ret3 = dateTime.atZone(ZoneOffset.UTC);
System.out.println(ret3); // 2100-04-14T15:20Z
int compareTo (ChronoLocalDateTime<?> other)
final var dateTime1 = LocalDateTime.of(2100, 1, 1, 0, 0);
final var dateTime2 = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime1); // 2100-01-01T00:00
System.out.println(dateTime2); // 2100-01-01T00:00
System.out.println(dateTime1.compareTo(dateTime2)); // 0
final var dateTime1 = LocalDateTime.of(2100, 4, 14, 15, 20);
final var dateTime2 = LocalDateTime.of(2100, 4, 14, 15, 21);
System.out.println(dateTime1); // 2100-04-14T15:20
System.out.println(dateTime2); // 2100-04-14T15:21
System.out.println(dateTime1.compareTo(dateTime2)); // -1
final var dateTime1 = LocalDateTime.of(2100, 4, 15, 0, 0);
final var dateTime2 = LocalDateTime.of(2100, 4, 14, 0, 0);
System.out.println(dateTime1); // 2100-04-15T00:00
System.out.println(dateTime2); // 2100-04-14T00:00
System.out.println(dateTime1.compareTo(dateTime2)); // 1
final var dateTime1 = LocalDateTime.of(2100, 1, 1, 0, 0);
final var dateTime2 = LocalDateTime.of(1900, 12, 30, 23, 59);
System.out.println(dateTime1); // 2100-01-01T00:00
System.out.println(dateTime2); // 1900-12-30T23:59
System.out.println(dateTime1.compareTo(dateTime2)); // 200
final var dateTime1 = LocalDateTime.of(2099, 1, 1, 0, 0, 1);
final var dateTime2 = LocalDateTime.of(2099, 1, 1, 0, 0);
System.out.println(dateTime1); // 2099-01-01T00:00:01
System.out.println(dateTime2); // 2099-01-01T00:00
System.out.println(dateTime1.compareTo(dateTime2)); // 1
final var dateTime1 = LocalDateTime.of(2100, 4, 13, 0, 0, 0, 123);
final var dateTime2 = LocalDateTime.of(2100, 4, 13, 0, 0, 0, 456);
System.out.println(dateTime1); // 2100-04-13T00:00:00.000000123
System.out.println(dateTime2); // 2100-04-13T00:00:00.000000456
System.out.println(dateTime1.compareTo(dateTime2)); // -1
boolean equals (Object obj)
final var dateTime1 = LocalDateTime.of(2100, 1, 1, 0, 0);
final var dateTime2 = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime1); // 2100-01-01T00:00
System.out.println(dateTime2); // 2100-01-01T00:00
System.out.println(dateTime1.equals(dateTime2)); // true
final var dateTime1 = LocalDateTime.of(2100, 4, 14, 15, 20);
final var dateTime2 = LocalDateTime.of(2100, 4, 14, 15, 21);
System.out.println(dateTime1); // 2100-04-14T15:20
System.out.println(dateTime2); // 2100-04-14T15:21
System.out.println(dateTime1.equals(dateTime2)); // false
final var dateTime1 = LocalDateTime.of(2100, 4, 15, 0, 0);
final var dateTime2 = LocalDateTime.of(2100, 4, 14, 0, 0);
System.out.println(dateTime1); // 2100-04-15T00:00
System.out.println(dateTime2); // 2100-04-14T00:00
System.out.println(dateTime1.equals(dateTime2)); // false
final var dateTime1 = LocalDateTime.of(2100, 1, 1, 0, 0);
final var dateTime2 = LocalDateTime.of(1900, 12, 30, 23, 59);
System.out.println(dateTime1); // 2100-01-01T00:00
System.out.println(dateTime2); // 1900-12-30T23:59
System.out.println(dateTime1.equals(dateTime2)); // false
final var dateTime1 = LocalDateTime.of(2099, 1, 1, 0, 0, 1);
final var dateTime2 = LocalDateTime.of(2099, 1, 1, 0, 0);
System.out.println(dateTime1); // 2099-01-01T00:00:01
System.out.println(dateTime2); // 2099-01-01T00:00
System.out.println(dateTime1.equals(dateTime2)); // false
final var dateTime1 = LocalDateTime.of(2100, 4, 13, 0, 0, 0, 123);
final var dateTime2 = LocalDateTime.of(2100, 4, 13, 0, 0, 0, 456);
System.out.println(dateTime1); // 2100-04-13T00:00:00.000000123
System.out.println(dateTime2); // 2100-04-13T00:00:00.000000456
System.out.println(dateTime1.equals(dateTime2)); // false
String format (DateTimeFormatter formatter)
final var dateTime = LocalDateTime.of(2100, 4, 7, 13, 30, 45);
System.out.println(dateTime); // 2100-04-07T13:30:45
{
final var ret1 = dateTime.format(
DateTimeFormatter.ISO_LOCAL_DATE_TIME);
System.out.println(ret1); // 2100-04-07T13:30:45
final var ret2 = dateTime.format(
DateTimeFormatter.ofPattern("yyyy年MM月dd日(E) HH時mm分ss秒"));
System.out.println(ret2); // 2100年04月07日(水) 13時30分45秒
}
{
System.out.println(Locale.getDefault().toLanguageTag()); // ja-JP
final var ret1 = dateTime.format(
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
System.out.println(ret1); // 2100/04/07 13:30:45
final var ret2 = dateTime.format(
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT));
System.out.println(ret2); // 2100/04/07 13:30
try {
final var ret = dateTime.format(
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG));
} catch (DateTimeException e) {
System.out.println("DateTimeException! : " + e.getMessage());
}
// 結果
// ↓
//DateTimeException! : Unable to extract ZoneId from temporal 2100-04-07T13:30:45
}
static LocalDateTime from (TemporalAccessor temporal)
final var zone = ZoneId.systemDefault();
System.out.println(zone); // Asia/Tokyo
final var temporal = ZonedDateTime.of(2100, 4, 13, 12, 30, 45, 1234, zone);
System.out.println(temporal); // 2100-04-13T12:30:45.000001234+09:00[Asia/Tokyo]
System.out.println(LocalDateTime.from(temporal)); // 2100-04-13T12:30:45.000001234
final var offset = ZoneOffset.ofHours(-8);
System.out.println(offset); // -08:00
final var temporal = OffsetDateTime.of(1900, 1, 2, 12, 30, 0, 0, offset);
System.out.println(temporal); // 1900-01-02T12:30-08:00
System.out.println(LocalDateTime.from(temporal)); // 1900-01-02T12:30
int get (TemporalField field)
final var dateTime = LocalDateTime.of(2100, 4, 13, 12, 30, 45, 123);
System.out.println(dateTime); // 2100-04-13T12:30:45.000000123
System.out.println(dateTime.isSupported(ChronoField.YEAR)); // true
System.out.println(dateTime.get(ChronoField.YEAR)); // 2100
System.out.println(dateTime.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(dateTime.get(ChronoField.MONTH_OF_YEAR)); // 4
System.out.println(dateTime.isSupported(ChronoField.DAY_OF_MONTH)); // true
System.out.println(dateTime.get(ChronoField.DAY_OF_MONTH)); // 13
System.out.println(dateTime.isSupported(ChronoField.HOUR_OF_DAY)); // true
System.out.println(dateTime.get(ChronoField.HOUR_OF_DAY)); // 12
System.out.println(dateTime.isSupported(ChronoField.MINUTE_OF_HOUR)); // true
System.out.println(dateTime.get(ChronoField.MINUTE_OF_HOUR)); // 30
System.out.println(dateTime.isSupported(ChronoField.SECOND_OF_MINUTE)); // true
System.out.println(dateTime.get(ChronoField.SECOND_OF_MINUTE)); // 45
System.out.println(dateTime.isSupported(ChronoField.NANO_OF_SECOND)); // true
System.out.println(dateTime.get(ChronoField.NANO_OF_SECOND)); // 123
final var dateTime = LocalDateTime.of(2100, 1, 1, 0, 0, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00
System.out.println(dateTime.isSupported(ChronoField.OFFSET_SECONDS)); // false
try {
final var ret = dateTime.get(ChronoField.OFFSET_SECONDS);
} catch (UnsupportedTemporalTypeException e) {
System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}
// 結果
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: OffsetSeconds
int getDayOfMonth ()
final var dateTime = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00
System.out.println(dateTime.getDayOfMonth()); // 1
final var dateTime = LocalDateTime.of(2100, 1, 2, 0, 0);
System.out.println(dateTime); // 2100-01-02T00:00
System.out.println(dateTime.getDayOfMonth()); // 2
final var dateTime = LocalDateTime.of(2100, 1, 3, 0, 0);
System.out.println(dateTime); // 2100-01-03T00:00
System.out.println(dateTime.getDayOfMonth()); // 3
final var dateTime = LocalDateTime.of(2100, 1, 30, 0, 0);
System.out.println(dateTime); // 2100-01-30T00:00
System.out.println(dateTime.getDayOfMonth()); // 30
final var dateTime = LocalDateTime.of(2100, 1, 31, 0, 0);
System.out.println(dateTime); // 2100-01-31T00:00
System.out.println(dateTime.getDayOfMonth()); // 31
final var dateTime = LocalDateTime.of(2100, 2, 1, 0, 0);
System.out.println(dateTime); // 2100-02-01T00:00
System.out.println(dateTime.getDayOfMonth()); // 1
DayOfWeek getDayOfWeek ()
final var dateTime = LocalDateTime.of(2100, 1, 3, 0, 0);
System.out.println(dateTime); // 2100-01-03T00:00
System.out.println(dateTime.getDayOfWeek()); // SUNDAY
final var dateTime = LocalDateTime.of(2100, 1, 4, 0, 0);
System.out.println(dateTime); // 2100-01-04T00:00
System.out.println(dateTime.getDayOfWeek()); // MONDAY
final var dateTime = LocalDateTime.of(2100, 1, 5, 0, 0);
System.out.println(dateTime); // 2100-01-05T00:00
System.out.println(dateTime.getDayOfWeek()); // TUESDAY
final var dateTime = LocalDateTime.of(2100, 1, 6, 0, 0);
System.out.println(dateTime); // 2100-01-06T00:00
System.out.println(dateTime.getDayOfWeek()); // WEDNESDAY
final var dateTime = LocalDateTime.of(2100, 1, 7, 0, 0);
System.out.println(dateTime); // 2100-01-07T00:00
System.out.println(dateTime.getDayOfWeek()); // THURSDAY
final var dateTime = LocalDateTime.of(2100, 1, 8, 0, 0);
System.out.println(dateTime); // 2100-01-08T00:00
System.out.println(dateTime.getDayOfWeek()); // FRIDAY
final var dateTime = LocalDateTime.of(2100, 1, 9, 0, 0);
System.out.println(dateTime); // 2100-01-09T00:00
System.out.println(dateTime.getDayOfWeek()); // SATURDAY
final var dateTime = LocalDateTime.of(2100, 1, 10, 0, 0);
System.out.println(dateTime); // 2100-01-10T00:00
System.out.println(dateTime.getDayOfWeek()); // SUNDAY
int getDayOfYear ()
final var dateTime = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00
System.out.println(dateTime.getDayOfYear()); // 1
final var dateTime = LocalDateTime.of(2100, 1, 2, 0, 0);
System.out.println(dateTime); // 2100-01-02T00:00
System.out.println(dateTime.getDayOfYear()); // 2
final var dateTime = LocalDateTime.of(2100, 1, 30, 0, 0);
System.out.println(dateTime); // 2100-01-30T00:00
System.out.println(dateTime.getDayOfYear()); // 30
final var dateTime = LocalDateTime.of(2100, 1, 31, 0, 0);
System.out.println(dateTime); // 2100-01-31T00:00
System.out.println(dateTime.getDayOfYear()); // 31
final var dateTime = LocalDateTime.of(2100, 2, 1, 0, 0);
System.out.println(dateTime); // 2100-02-01T00:00
System.out.println(dateTime.getDayOfYear()); // 32
final var dateTime = LocalDateTime.of(2100, 2, 2, 0, 0);
System.out.println(dateTime); // 2100-02-02T00:00
System.out.println(dateTime.getDayOfYear()); // 33
final var dateTime = LocalDateTime.of(2100, 12, 31, 0, 0);
System.out.println(dateTime); // 2100-12-31T00:00
System.out.println(dateTime.getDayOfYear()); // 365
final var dateTime = LocalDateTime.of(2101, 1, 1, 0, 0);
System.out.println(dateTime); // 2101-01-01T00:00
System.out.println(dateTime.getDayOfYear()); // 1
int getHour ()
final var dateTime = LocalDateTime.of(2100, 1, 1, 14, 30, 15);
System.out.println(dateTime); // 2100-01-01T14:30:15
System.out.println(dateTime.getHour()); // 14
long getLong (TemporalField field)
final var dateTime = LocalDateTime.of(2100, 4, 13, 12, 30, 45, 123);
System.out.println(dateTime); // 2100-04-13T12:30:45.000000123
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
int getMinute ()
final var dateTime = LocalDateTime.of(2100, 1, 1, 13, 30, 15);
System.out.println(dateTime); // 2100-01-01T13:30:15
System.out.println(dateTime.getMinute()); // 30
Month getMonth ()
final var dateTime = LocalDateTime.of(2100, 1, 15, 0, 0);
System.out.println(dateTime); // 2100-01-15T00:00
System.out.println(dateTime.getMonth()); // JANUARY
final var dateTime = LocalDateTime.of(2100, 2, 15, 0, 0);
System.out.println(dateTime); // 2100-02-15T00:00
System.out.println(dateTime.getMonth()); // FEBRUARY
final var dateTime = LocalDateTime.of(2100, 3, 15, 0, 0);
System.out.println(dateTime); // 2100-03-15T00:00
System.out.println(dateTime.getMonth()); // MARCH
final var dateTime = LocalDateTime.of(2100, 12, 15, 0, 0);
System.out.println(dateTime); // 2100-12-15T00:00
System.out.println(dateTime.getMonth()); // DECEMBER
final var dateTime = LocalDateTime.of(2101, 1, 1, 0, 0);
System.out.println(dateTime); // 2101-01-01T00:00
System.out.println(dateTime.getMonth()); // JANUARY
int getMonthValue ()
final var dateTime = LocalDateTime.of(2100, 1, 15, 0, 0);
System.out.println(dateTime); // 2100-01-15T00:00
System.out.println(dateTime.getMonthValue()); // 1
final var dateTime = LocalDateTime.of(2100, 2, 15, 0, 0);
System.out.println(dateTime); // 2100-02-15T00:00
System.out.println(dateTime.getMonthValue()); // 2
final var dateTime = LocalDateTime.of(2100, 3, 15, 0, 0);
System.out.println(dateTime); // 2100-03-15T00:00
System.out.println(dateTime.getMonthValue()); // 3
final var dateTime = LocalDateTime.of(2100, 12, 15, 0, 0);
System.out.println(dateTime); // 2100-12-15T00:00
System.out.println(dateTime.getMonthValue()); // 12
final var dateTime = LocalDateTime.of(2101, 1, 1, 0, 0);
System.out.println(dateTime); // 2101-01-01T00:00
System.out.println(dateTime.getMonthValue()); // 1
int getNano ()
final var dateTime = LocalDateTime.of(2100, 1, 1, 13, 30, 15);
System.out.println(dateTime); // 2100-01-01T13:30:15
System.out.println(dateTime.getNano()); // 0
final var dateTime = LocalDateTime.of(2100, 1, 1, 13, 30, 15, 123456789);
System.out.println(dateTime); // 2100-01-01T13:30:15.123456789
System.out.println(dateTime.getNano()); // 123456789
int getSecond ()
final var dateTime = LocalDateTime.of(2100, 1, 1, 13, 30, 15);
System.out.println(dateTime); // 2100-01-01T13:30:15
System.out.println(dateTime.getSecond()); // 15
int getYear ()
final var dateTime = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00
System.out.println(dateTime.getYear()); // 2100
final var dateTime = LocalDateTime.of(1900, 1, 1, 0, 0);
System.out.println(dateTime); // 1900-01-01T00:00
System.out.println(dateTime.getYear()); // 1900
final var dateTime = LocalDateTime.of(-3000, 1, 1, 0, 0);
System.out.println(dateTime); // -3000-01-01T00:00
System.out.println(dateTime.getYear()); // -3000
final var dateTime = LocalDateTime.of(9999, 1, 1, 0, 0);
System.out.println(dateTime); // 9999-01-01T00:00
System.out.println(dateTime.getYear()); // 9999
int hashCode ()
final var ret1 = LocalDateTime.of(2100, 1, 1, 0, 0).hashCode();
System.out.println(ret1); // 4302913
final var ret2 = LocalDateTime.of(1900, 4, 13, 12, 30, 40, 123).hashCode();
System.out.println(ret2); // -1324865152
final var ret3 = LocalDateTime.MIN.hashCode();
System.out.println(ret3); // -304793535
final var ret4 = LocalDateTime.MAX.hashCode();
System.out.println(ret4); // 2090566260
boolean isAfter (ChronoLocalDateTime<?> other)
final var dateTime = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00
final var dateTime2 = LocalDateTime.of(2099, 1, 1, 0, 0);
final var dateTime3 = LocalDateTime.of(2100, 1, 1, 0, 0);
final var dateTime4 = LocalDateTime.of(2101, 1, 1, 0, 0);
System.out.println(dateTime2); // 2099-01-01T00:00
System.out.println(dateTime3); // 2100-01-01T00:00
System.out.println(dateTime4); // 2101-01-01T00:00
System.out.println(dateTime.isAfter(dateTime2)); // true
System.out.println(dateTime.isAfter(dateTime3)); // false
System.out.println(dateTime.isAfter(dateTime4)); // false
final var dateTime = LocalDateTime.of(2100, 1, 1, 13, 0);
System.out.println(dateTime); // 2100-01-01T13:00
final var dateTime2 = LocalDateTime.of(2100, 1, 1, 13, 0);
final var dateTime3 = LocalDateTime.of(2100, 1, 1, 13, 0, 1);
final var dateTime4 = LocalDateTime.of(2100, 1, 1, 12, 59);
System.out.println(dateTime2); // 2100-01-01T13:00
System.out.println(dateTime3); // 2100-01-01T13:00:01
System.out.println(dateTime4); // 2100-01-01T12:59
System.out.println(dateTime.isAfter(dateTime2)); // false
System.out.println(dateTime.isAfter(dateTime3)); // false
System.out.println(dateTime.isAfter(dateTime4)); // true
boolean isBefore (ChronoLocalDateTime<?> other)
final var dateTime = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00
final var dateTime2 = LocalDateTime.of(2099, 1, 1, 0, 0);
final var dateTime3 = LocalDateTime.of(2100, 1, 1, 0, 0);
final var dateTime4 = LocalDateTime.of(2101, 1, 1, 0, 0);
System.out.println(dateTime2); // 2099-01-01T00:00
System.out.println(dateTime3); // 2100-01-01T00:00
System.out.println(dateTime4); // 2101-01-01T00:00
System.out.println(dateTime.isBefore(dateTime2)); // false
System.out.println(dateTime.isBefore(dateTime3)); // false
System.out.println(dateTime.isBefore(dateTime4)); // true
final var dateTime = LocalDateTime.of(2100, 1, 1, 13, 0);
System.out.println(dateTime); // 2100-01-01T13:00
final var dateTime2 = LocalDateTime.of(2100, 1, 1, 13, 0);
final var dateTime3 = LocalDateTime.of(2100, 1, 1, 13, 0, 1);
final var dateTime4 = LocalDateTime.of(2100, 1, 1, 12, 59);
System.out.println(dateTime2); // 2100-01-01T13:00
System.out.println(dateTime3); // 2100-01-01T13:00:01
System.out.println(dateTime4); // 2100-01-01T12:59
System.out.println(dateTime.isBefore(dateTime2)); // false
System.out.println(dateTime.isBefore(dateTime3)); // true
System.out.println(dateTime.isBefore(dateTime4)); // false
boolean isEqual (ChronoLocalDateTime<?> other)
final var dateTime1 = LocalDateTime.of(2100, 6, 30, 12, 0);
final var dateTime2 = LocalDateTime.of(2100, 7, 1, 12, 0);
System.out.println(dateTime1); // 2100-06-30T12:00
System.out.println(dateTime2); // 2100-07-01T12:00
System.out.println(dateTime1.isEqual(dateTime2)); // false
System.out.println(dateTime1.isEqual(dateTime1)); // true
System.out.println(dateTime2.isEqual(dateTime1)); // false
final var dateTime1 = LocalDateTime.of(1900, 1, 1, 13, 0);
final var dateTime2 = LocalDateTime.of(1900, 1, 1, 12, 59);
System.out.println(dateTime1); // 1900-01-01T13:00
System.out.println(dateTime2); // 1900-01-01T12:59
System.out.println(dateTime1.isEqual(dateTime2)); // false
System.out.println(dateTime1.isEqual(dateTime1)); // true
System.out.println(dateTime2.isEqual(dateTime1)); // false
boolean isSupported (TemporalField field)
final var dateTime = LocalDateTime.of(2100, 4, 13, 12, 30, 45, 123);
System.out.println(dateTime); // 2100-04-13T12:30:45.000000123
System.out.println(dateTime.isSupported(ChronoField.YEAR)); // true
System.out.println(dateTime.get(ChronoField.YEAR)); // 2100
System.out.println(dateTime.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(dateTime.get(ChronoField.MONTH_OF_YEAR)); // 4
System.out.println(dateTime.isSupported(ChronoField.DAY_OF_MONTH)); // true
System.out.println(dateTime.get(ChronoField.DAY_OF_MONTH)); // 13
System.out.println(dateTime.isSupported(ChronoField.HOUR_OF_DAY)); // true
System.out.println(dateTime.get(ChronoField.HOUR_OF_DAY)); // 12
System.out.println(dateTime.isSupported(ChronoField.MINUTE_OF_HOUR)); // true
System.out.println(dateTime.get(ChronoField.MINUTE_OF_HOUR)); // 30
System.out.println(dateTime.isSupported(ChronoField.SECOND_OF_MINUTE)); // true
System.out.println(dateTime.get(ChronoField.SECOND_OF_MINUTE)); // 45
System.out.println(dateTime.isSupported(ChronoField.NANO_OF_SECOND)); // true
System.out.println(dateTime.get(ChronoField.NANO_OF_SECOND)); // 123
final var dateTime = LocalDateTime.of(2100, 1, 1, 0, 0, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00
System.out.println(dateTime.isSupported(ChronoField.OFFSET_SECONDS)); // false
try {
final var ret = dateTime.get(ChronoField.OFFSET_SECONDS);
} catch (UnsupportedTemporalTypeException e) {
System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}
// 結果
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: OffsetSeconds
boolean isSupported (TemporalUnit unit)
final var dateTime = LocalDateTime.of(2100, 4, 13, 12, 30, 45, 123);
System.out.println(dateTime); // 2100-04-13T12:30:45.000000123
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
LocalDateTime minus (long amountToSubtract, TemporalUnit unit)
final var dateTime = LocalDateTime.of(2100, 4, 13, 0, 0);
System.out.println(dateTime); // 2100-04-13T00:00
final var ret1 = dateTime.minus(1, ChronoUnit.YEARS);
System.out.println(ret1); // 2099-04-13T00:00
final var ret2 = dateTime.minus(2, ChronoUnit.MONTHS);
System.out.println(ret2); // 2100-02-13T00:00
final var ret3 = dateTime.minus(3, ChronoUnit.DAYS);
System.out.println(ret3); // 2100-04-10T00:00
final var dateTime = LocalDateTime.of(2100, 1, 1, 10, 20, 30, 123);
System.out.println(dateTime); // 2100-01-01T10:20:30.000000123
final var ret1 = dateTime.minus(1, ChronoUnit.HOURS);
System.out.println(ret1); // 2100-01-01T09:20:30.000000123
final var ret2 = dateTime.minus(2, ChronoUnit.MINUTES);
System.out.println(ret2); // 2100-01-01T10:18:30.000000123
final var ret3 = dateTime.minus(3, ChronoUnit.SECONDS);
System.out.println(ret3); // 2100-01-01T10:20:27.000000123
final var ret4 = dateTime.minus(4, ChronoUnit.NANOS);
System.out.println(ret4); // 2100-01-01T10:20:30.000000119
final var dateTime = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00
final var ret1 = dateTime.minus(1, ChronoUnit.HOURS);
System.out.println(ret1); // 2099-12-31T23:00
final var ret2 = dateTime.minus(365, ChronoUnit.DAYS);
System.out.println(ret2); // 2099-01-01T00:00
final var ret3 = dateTime.minus(3000, ChronoUnit.YEARS);
System.out.println(ret3); // -0900-01-01T00:00
LocalDateTime minus (TemporalAmount amountToSubtract)
final var dateTime = LocalDateTime.of(2100, 4, 13, 0, 0);
System.out.println(dateTime); // 2100-04-13T00:00
final var ret1 = dateTime.minus(Period.ofYears(1));
System.out.println(ret1); // 2099-04-13T00:00
final var ret2 = dateTime.minus(Period.ofMonths(2));
System.out.println(ret2); // 2100-02-13T00:00
final var ret3 = dateTime.minus(Period.ofDays(3));
System.out.println(ret3); // 2100-04-10T00:00
final var dateTime = LocalDateTime.of(2100, 1, 1, 10, 20, 30, 123);
System.out.println(dateTime); // 2100-01-01T10:20:30.000000123
final var ret1 = dateTime.minus(Duration.ofHours(1));
System.out.println(ret1); // 2100-01-01T09:20:30.000000123
final var ret2 = dateTime.minus(Duration.ofMinutes(2));
System.out.println(ret2); // 2100-01-01T10:18:30.000000123
final var ret3 = dateTime.minus(Duration.ofSeconds(3));
System.out.println(ret3); // 2100-01-01T10:20:27.000000123
final var ret4 = dateTime.minus(Duration.ofNanos(4));
System.out.println(ret4); // 2100-01-01T10:20:30.000000119
final var dateTime = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00
final var ret1 = dateTime.minus(Duration.ofHours(1));
System.out.println(ret1); // 2099-12-31T23:00
final var ret2 = dateTime.minus(Period.ofDays(365));
System.out.println(ret2); // 2099-01-01T00:00
final var ret3 = dateTime.minus(Period.ofYears(3000));
System.out.println(ret3); // -0900-01-01T00:00
LocalDateTime minusDays (long days)
final var dateTime = LocalDateTime.of(2100, 5, 20, 0, 0);
System.out.println(dateTime); // 2100-05-20T00:00
System.out.println(dateTime.minusDays(1)); // 2100-05-19T00:00
System.out.println(dateTime.minusDays(2)); // 2100-05-18T00:00
System.out.println(dateTime.minusDays(20)); // 2100-04-30T00:00
System.out.println(dateTime.minusDays(365)); // 2099-05-20T00:00
final var dateTime = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00
System.out.println(dateTime.minusDays(1)); // 2099-12-31T00:00
LocalDateTime minusHours (long hours)
final var dateTime = LocalDateTime.of(2100, 1, 1, 13, 30, 15);
System.out.println(dateTime); // 2100-01-01T13:30:15
System.out.println(dateTime.minusHours(1)); // 2100-01-01T12:30:15
System.out.println(dateTime.minusHours(2)); // 2100-01-01T11:30:15
System.out.println(dateTime.minusHours(13)); // 2100-01-01T00:30:15
System.out.println(dateTime.minusHours(14)); // 2099-12-31T23:30:15
LocalDateTime minusMinutes (long minutes)
final var dateTime = LocalDateTime.of(2100, 1, 1, 13, 30);
System.out.println(dateTime); // 2100-01-01T13:30
System.out.println(dateTime.minusMinutes(1)); // 2100-01-01T13:29
System.out.println(dateTime.minusMinutes(2)); // 2100-01-01T13:28
System.out.println(dateTime.minusMinutes(30)); // 2100-01-01T13:00
System.out.println(dateTime.minusMinutes(31)); // 2100-01-01T12:59
System.out.println(dateTime.minusMinutes(60 * 14)); // 2099-12-31T23:30
LocalDateTime minusMonths (long months)
final var dateTime = LocalDateTime.of(2100, 5, 20, 0, 0);
System.out.println(dateTime); // 2100-05-20T00:00
System.out.println(dateTime.minusMonths(1)); // 2100-04-20T00:00
System.out.println(dateTime.minusMonths(2)); // 2100-03-20T00:00
System.out.println(dateTime.minusMonths(12)); // 2099-05-20T00:00
final var dateTime = LocalDateTime.of(2101, 3, 31, 0, 0);
System.out.println(dateTime); // 2101-03-31T00:00
System.out.println(dateTime.minusMonths(1)); // 2101-02-28T00:00
LocalDateTime minusNanos (long nanos)
final var dateTime = LocalDateTime.of(2100, 1, 1, 13, 30, 15, 123);
System.out.println(dateTime); // 2100-01-01T13:30:15.000000123
System.out.println(dateTime.minusNanos(1)); // 2100-01-01T13:30:15.000000122
System.out.println(dateTime.minusNanos(2)); // 2100-01-01T13:30:15.000000121
System.out.println(dateTime.minusNanos(123)); // 2100-01-01T13:30:15
System.out.println(dateTime.minusNanos(124)); // 2100-01-01T13:30:14.999999999
LocalDateTime minusSeconds (long seconds)
final var dateTime = LocalDateTime.of(2100, 1, 1, 13, 30, 15);
System.out.println(dateTime); // 2100-01-01T13:30:15
System.out.println(dateTime.minusSeconds(1)); // 2100-01-01T13:30:14
System.out.println(dateTime.minusSeconds(2)); // 2100-01-01T13:30:13
System.out.println(dateTime.minusSeconds(15)); // 2100-01-01T13:30
System.out.println(dateTime.minusSeconds(16)); // 2100-01-01T13:29:59
System.out.println(dateTime.minusSeconds(60 * 60)); // 2100-01-01T12:30:15
System.out.println(dateTime.minusSeconds(60 * 60 * 24)); // 2099-12-31T13:30:15
LocalDateTime minusWeeks (long weeks)
final var dateTime = LocalDateTime.of(2100, 5, 20, 0, 0);
System.out.println(dateTime); // 2100-05-20T00:00
System.out.println(dateTime.minusWeeks(1)); // 2100-05-13T00:00
System.out.println(dateTime.minusWeeks(2)); // 2100-05-06T00:00
System.out.println(dateTime.minusWeeks(20)); // 2099-12-31T00:00
System.out.println(dateTime.minusWeeks(100)); // 2098-06-19T00:00
final var dateTime = LocalDateTime.of(2100, 1, 7, 0, 0);
System.out.println(dateTime); // 2100-01-07T00:00
System.out.println(dateTime.minusWeeks(1)); // 2099-12-31T00:00
LocalDateTime minusYears (long years)
final var dateTime = LocalDateTime.of(2103, 5, 20, 0, 0);
System.out.println(dateTime); // 2103-05-20T00:00
System.out.println(dateTime.minusYears(1)); // 2102-05-20T00:00
System.out.println(dateTime.minusYears(2)); // 2101-05-20T00:00
System.out.println(dateTime.minusYears(10)); // 2093-05-20T00:00
final var dateTime = LocalDateTime.of(2104, 2, 29, 0, 0);
System.out.println(dateTime); // 2104-02-29T00:00
System.out.println(dateTime.minusYears(1)); // 2103-02-28T00:00
static LocalDateTime now ()
final var dateTime = LocalDateTime.now();
System.out.println(dateTime); // 2024-05-18T16:34:31.603367500
static LocalDateTime now (Clock clock)
// 意図的に5日進めた Clock です。
final var clock = Clock.offset(Clock.systemDefaultZone(), Duration.ofDays(5));
System.out.println(LocalDateTime.now()); // 2024-05-18T16:35:07.450483800
System.out.println(LocalDateTime.now(clock)); // 2024-05-23T16:35:07.451482800
static LocalDateTime now (ZoneId zone)
final var zone1 = ZoneId.systemDefault();
System.out.println(zone1); // Asia/Tokyo
final var zone2 = ZoneId.of("America/Los_Angeles");
System.out.println(zone2); // America/Los_Angeles
System.out.println(LocalDateTime.now(zone1)); // 2024-05-18T16:36:51.435274500
System.out.println(LocalDateTime.now(zone2)); // 2024-05-18T00:36:51.435274500
static LocalDateTime of (int year, int month, int dayOfMonth, int hour, int minute)
System.out.println(LocalDateTime.of(2100, 4, 14, 13, 30)); // 2100-04-14T13:30
System.out.println(LocalDateTime.of(1900, 2, 28, 0, 0)); // 1900-02-28T00:00
System.out.println(LocalDateTime.of(-999, 12, 1, 23, 59)); // -0999-12-01T23:59
static LocalDateTime of (int year, int month, int dayOfMonth, int hour, int minute, int second)
System.out.println(LocalDateTime.of(2100, 4, 14, 13, 30, 10)); // 2100-04-14T13:30:10
System.out.println(LocalDateTime.of(1900, 2, 28, 0, 0, 0)); // 1900-02-28T00:00
System.out.println(LocalDateTime.of(-999, 12, 1, 23, 59, 59)); // -0999-12-01T23:59:59
static LocalDateTime of (int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
final var dateTime1 = LocalDateTime.of(2100, 4, 14, 13, 30, 10, 123);
System.out.println(dateTime1); // 2100-04-14T13:30:10.000000123
final var dateTime2 = LocalDateTime.of(1900, 2, 28, 0, 0, 0, 0);
System.out.println(dateTime2); // 1900-02-28T00:00
final var dateTime3 = LocalDateTime.of(-999, 12, 1, 23, 59, 59, 999999999);
System.out.println(dateTime3); // -0999-12-01T23:59:59.999999999
static LocalDateTime of (int year, Month month, int dayOfMonth, int hour, int minute)
final var dateTime1 = LocalDateTime.of(2100, Month.APRIL, 14, 13, 30);
System.out.println(dateTime1); // 2100-04-14T13:30
final var dateTime2 = LocalDateTime.of(1900, Month.FEBRUARY, 28, 0, 0);
System.out.println(dateTime2); // 1900-02-28T00:00
final var dateTime3 = LocalDateTime.of(-999, Month.DECEMBER, 1, 23, 59);
System.out.println(dateTime3); // -0999-12-01T23:59
static LocalDateTime of (int year, Month month, int dayOfMonth, int hour, int minute, int second)
final var dateTime1 = LocalDateTime.of(2100, Month.APRIL, 14, 13, 30, 10);
System.out.println(dateTime1); // 2100-04-14T13:30:10
final var dateTime2 = LocalDateTime.of(1900, Month.FEBRUARY, 28, 0, 0, 0);
System.out.println(dateTime2); // 1900-02-28T00:00
final var dateTime3 = LocalDateTime.of(-999, Month.DECEMBER, 1, 23, 59, 59);
System.out.println(dateTime3); // -0999-12-01T23:59:59
static LocalDateTime of (int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
final var dateTime1 = LocalDateTime.of(2100, Month.APRIL, 14, 13, 30, 10, 123);
System.out.println(dateTime1); // 2100-04-14T13:30:10.000000123
final var dateTime2 = LocalDateTime.of(1900, Month.FEBRUARY, 28, 0, 0, 0, 0);
System.out.println(dateTime2); // 1900-02-28T00:00
final var dateTime3 = LocalDateTime.of(-999, Month.DECEMBER, 1, 23, 59, 59, 999999999);
System.out.println(dateTime3); // -0999-12-01T23:59:59.999999999
static LocalDateTime of (LocalDate date, LocalTime time)
final var date = LocalDate.of(2100, Month.APRIL, 14);
System.out.println(date); // 2100-04-14
final var time = LocalTime.of(13, 30, 10);
System.out.println(time); // 13:30:10
System.out.println(LocalDateTime.of(date, time)); // 2100-04-14T13:30:10
final var date = LocalDate.of(1900, Month.FEBRUARY, 28);
System.out.println(date); // 1900-02-28
System.out.println(LocalDateTime.of(date, LocalTime.MIN)); // 1900-02-28T00:00
static LocalDateTime ofEpochSecond (long epochSecond, int nanoOfSecond, ZoneOffset offset)
final var epochSecond = 4102444800L;
System.out.println(Instant.ofEpochSecond(epochSecond)); // 2100-01-01T00:00:00Z
final var dateTime1 = LocalDateTime.ofEpochSecond(epochSecond, 0, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-01-01T00:00
final var dateTime2 = LocalDateTime.ofEpochSecond(epochSecond, 0, ZoneOffset.ofHours(-9));
System.out.println(dateTime2); // 2099-12-31T15:00
final var dateTime3 = LocalDateTime.ofEpochSecond(epochSecond, 123, ZoneOffset.UTC);
System.out.println(dateTime3); // 2100-01-01T00:00:00.000000123
static LocalDateTime ofInstant (Instant instant, ZoneId zone)
final var instant = Instant.ofEpochSecond(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z
final var dateTime1 = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-01-01T00:00
final var zoneId = ZoneId.systemDefault();
System.out.println(zoneId); // Asia/Tokyo
final var dateTime2 = LocalDateTime.ofInstant(instant, zoneId);
System.out.println(dateTime2); // 2100-01-01T09:00
static LocalDateTime parse (CharSequence text)
final var ret1 = LocalDateTime.parse("2100-04-14T00:00");
System.out.println(ret1); // 2100-04-14T00:00
final var ret2 = LocalDateTime.parse("2100-04-14T13:30:45");
System.out.println(ret2); // 2100-04-14T13:30:45
final var ret3 = LocalDateTime.parse("2100-04-14T13:30:45.123456789");
System.out.println(ret3); // 2100-04-14T13:30:45.123456789
try {
final var ret = LocalDateTime.parse("2100/04/14 00:00");
} catch (DateTimeParseException e) {
System.out.println("DateTimeParseException! : " + e.getMessage());
}
// 結果
// ↓
//DateTimeParseException! : Text '2100/04/14 00:00' could not be parsed at index 4
static LocalDateTime parse (CharSequence text, DateTimeFormatter formatter)
final var ret1 = LocalDateTime.parse(
"2100-04-14T12:30:45", DateTimeFormatter.ISO_LOCAL_DATE_TIME);
System.out.println(ret1); // 2100-04-14T12:30:45
final var ret2 = LocalDateTime.parse(
"2100年4月14日12時30分45秒", DateTimeFormatter.ofPattern("y年M月d日H時m分s秒"));
System.out.println(ret2); // 2100-04-14T12:30:45
System.out.println(Locale.getDefault().toLanguageTag()); // ja-JP
final var ret1 = LocalDateTime.parse(
"2100/04/14 12:30:45",
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
System.out.println(ret1); // 2100-04-14T12:30:45
final var ret2 = LocalDateTime.parse(
"2100/04/14 12:30",
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT));
System.out.println(ret2); // 2100-04-14T12:30
LocalDateTime plus (long amountToAdd, TemporalUnit unit)
final var dateTime = LocalDateTime.of(2100, 4, 13, 0, 0);
System.out.println(dateTime); // 2100-04-13T00:00
final var ret1 = dateTime.plus(1, ChronoUnit.YEARS);
System.out.println(ret1); // 2101-04-13T00:00
final var ret2 = dateTime.plus(2, ChronoUnit.MONTHS);
System.out.println(ret2); // 2100-06-13T00:00
final var ret3 = dateTime.plus(3, ChronoUnit.DAYS);
System.out.println(ret3); // 2100-04-16T00:00
final var dateTime = LocalDateTime.of(2100, 1, 1, 10, 20, 30, 123);
System.out.println(dateTime); // 2100-01-01T10:20:30.000000123
final var ret1 = dateTime.plus(1, ChronoUnit.HOURS);
System.out.println(ret1); // 2100-01-01T11:20:30.000000123
final var ret2 = dateTime.plus(2, ChronoUnit.MINUTES);
System.out.println(ret2); // 2100-01-01T10:22:30.000000123
final var ret3 = dateTime.plus(3, ChronoUnit.SECONDS);
System.out.println(ret3); // 2100-01-01T10:20:33.000000123
final var ret4 = dateTime.plus(4, ChronoUnit.NANOS);
System.out.println(ret4); // 2100-01-01T10:20:30.000000127
final var dateTime = LocalDateTime.of(2100, 12, 31, 23, 0);
System.out.println(dateTime); // 2100-12-31T23:00
final var ret1 = dateTime.plus(1, ChronoUnit.HOURS);
System.out.println(ret1); // 2101-01-01T00:00
final var ret2 = dateTime.plus(366, ChronoUnit.DAYS);
System.out.println(ret2); // 2102-01-01T23:00
final var ret3 = dateTime.plus(3000, ChronoUnit.YEARS);
System.out.println(ret3); // 5100-12-31T23:00
LocalDateTime plus (TemporalAmount amountToAdd)
final var dateTime = LocalDateTime.of(2100, 4, 13, 0, 0);
System.out.println(dateTime); // 2100-04-13T00:00
final var ret1 = dateTime.plus(Period.ofYears(1));
System.out.println(ret1); // 2101-04-13T00:00
final var ret2 = dateTime.plus(Period.ofMonths(2));
System.out.println(ret2); // 2100-06-13T00:00
final var ret3 = dateTime.plus(Period.ofDays(3));
System.out.println(ret3); // 2100-04-16T00:00
final var dateTime = LocalDateTime.of(2100, 1, 1, 10, 20, 30, 123);
System.out.println(dateTime); // 2100-01-01T10:20:30.000000123
final var ret1 = dateTime.plus(Duration.ofHours(1));
System.out.println(ret1); // 2100-01-01T11:20:30.000000123
final var ret2 = dateTime.plus(Duration.ofMinutes(2));
System.out.println(ret2); // 2100-01-01T10:22:30.000000123
final var ret3 = dateTime.plus(Duration.ofSeconds(3));
System.out.println(ret3); // 2100-01-01T10:20:33.000000123
final var ret4 = dateTime.plus(Duration.ofNanos(4));
System.out.println(ret4); // 2100-01-01T10:20:30.000000127
final var dateTime = LocalDateTime.of(2100, 12, 31, 23, 0);
System.out.println(dateTime); // 2100-12-31T23:00
final var ret1 = dateTime.plus(Duration.ofHours(1));
System.out.println(ret1); // 2101-01-01T00:00
final var ret2 = dateTime.plus(Period.ofDays(366));
System.out.println(ret2); // 2102-01-01T23:00
final var ret3 = dateTime.plus(Period.ofYears(3000));
System.out.println(ret3); // 5100-12-31T23:00
LocalDateTime plusDays (long days)
final var dateTime = LocalDateTime.of(2100, 5, 20, 0, 0);
System.out.println(dateTime); // 2100-05-20T00:00
System.out.println(dateTime.plusDays(1)); // 2100-05-21T00:00
System.out.println(dateTime.plusDays(2)); // 2100-05-22T00:00
System.out.println(dateTime.plusDays(12)); // 2100-06-01T00:00
System.out.println(dateTime.plusDays(365)); // 2101-05-20T00:00
final var dateTime = LocalDateTime.of(2099, 12, 31, 0, 0);
System.out.println(dateTime); // 2099-12-31T00:00
System.out.println(dateTime.plusDays(1)); // 2100-01-01T00:00
LocalDateTime plusHours (long hours)
final var dateTime = LocalDateTime.of(2100, 1, 1, 13, 0);
System.out.println(dateTime); // 2100-01-01T13:00
System.out.println(dateTime.plusHours(1)); // 2100-01-01T14:00
System.out.println(dateTime.plusHours(2)); // 2100-01-01T15:00
System.out.println(dateTime.plusHours(11)); // 2100-01-02T00:00
System.out.println(dateTime.plusHours(12)); // 2100-01-02T01:00
LocalDateTime plusMinutes (long minutes)
final var dateTime = LocalDateTime.of(2100, 1, 1, 13, 30);
System.out.println(dateTime); // 2100-01-01T13:30
System.out.println(dateTime.plusMinutes(1)); // 2100-01-01T13:31
System.out.println(dateTime.plusMinutes(2)); // 2100-01-01T13:32
System.out.println(dateTime.plusMinutes(30)); // 2100-01-01T14:00
System.out.println(dateTime.plusMinutes(31)); // 2100-01-01T14:01
System.out.println(dateTime.plusMinutes(60 * 24)); // 2100-01-02T13:30
System.out.println(dateTime.plusMinutes(60 * 24 * 365)); // 2101-01-01T13:30
LocalDateTime plusMonths (long months)
final var dateTime = LocalDateTime.of(2100, 5, 20, 0, 0);
System.out.println(dateTime); // 2100-05-20T00:00
System.out.println(dateTime.plusMonths(1)); // 2100-06-20T00:00
System.out.println(dateTime.plusMonths(2)); // 2100-07-20T00:00
System.out.println(dateTime.plusMonths(12)); // 2101-05-20T00:00
final var dateTime = LocalDateTime.of(2101, 3, 31, 0, 0);
System.out.println(dateTime); // 2101-03-31T00:00
System.out.println(dateTime.plusMonths(1)); // 2101-04-30T00:00
LocalDateTime plusNanos (long nanos)
final var dateTime = LocalDateTime.of(2100, 1, 1, 13, 30, 15, 123);
System.out.println(dateTime); // 2100-01-01T13:30:15.000000123
System.out.println(dateTime.plusNanos(1)); // 2100-01-01T13:30:15.000000124
System.out.println(dateTime.plusNanos(2)); // 2100-01-01T13:30:15.000000125
final var ret1 = dateTime.plusNanos(1000000000L - 124);
System.out.println(ret1); // 2100-01-01T13:30:15.999999999
final var ret2 = dateTime.plusNanos(1000000000L - 123);
System.out.println(ret2); // 2100-01-01T13:30:16
LocalDateTime plusSeconds (long seconds)
final var dateTime = LocalDateTime.of(2100, 1, 1, 13, 30, 15);
System.out.println(dateTime); // 2100-01-01T13:30:15
System.out.println(dateTime.plusSeconds(1)); // 2100-01-01T13:30:16
System.out.println(dateTime.plusSeconds(2)); // 2100-01-01T13:30:17
System.out.println(dateTime.plusSeconds(45)); // 2100-01-01T13:31
System.out.println(dateTime.plusSeconds(46)); // 2100-01-01T13:31:01
System.out.println(dateTime.plusSeconds(60 + 45)); // 2100-01-01T13:32
System.out.println(dateTime.plusSeconds(60 * 60)); // 2100-01-01T14:30:15
System.out.println(dateTime.plusSeconds(60 * 60 * 24)); // 2100-01-02T13:30:15
LocalDateTime plusWeeks (long weeks)
final var dateTime = LocalDateTime.of(2100, 5, 20, 0, 0);
System.out.println(dateTime); // 2100-05-20T00:00
System.out.println(dateTime.plusWeeks(1)); // 2100-05-27T00:00
System.out.println(dateTime.plusWeeks(2)); // 2100-06-03T00:00
System.out.println(dateTime.plusWeeks(20)); // 2100-10-07T00:00
System.out.println(dateTime.plusWeeks(100)); // 2102-04-20T00:00
final var dateTime = LocalDateTime.of(2100, 12, 31, 0, 0);
System.out.println(dateTime); // 2100-12-31T00:00
System.out.println(dateTime.plusWeeks(1)); // 2101-01-07T00:00
LocalDateTime plusYears (long years)
final var dateTime = LocalDateTime.of(2103, 5, 20, 0, 0);
System.out.println(dateTime); // 2103-05-20T00:00
System.out.println(dateTime.plusYears(1)); // 2104-05-20T00:00
System.out.println(dateTime.plusYears(2)); // 2105-05-20T00:00
System.out.println(dateTime.plusYears(10)); // 2113-05-20T00:00
final var dateTime = LocalDateTime.of(2104, 2, 29, 0, 0);
System.out.println(dateTime); // 2104-02-29T00:00
System.out.println(dateTime.plusYears(1)); // 2105-02-28T00:00
<R> R query (TemporalQuery<R> query)
final var dateTime = LocalDateTime.of(2100, 4, 14, 12, 30);
System.out.println(dateTime); // 2100-04-14T12:30
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.precision())); // Nanos
ValueRange range (TemporalField field)
final var dateTime = LocalDateTime.of(2100, 4, 14, 0, 0);
System.out.println(dateTime); // 2100-04-14T00:00
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
final var dateTime1 = LocalDateTime.of(2103, 2, 1, 0, 0);
System.out.println(dateTime1); // 2103-02-01T00:00
System.out.println(dateTime1.range(ChronoField.DAY_OF_MONTH)); // 1 - 28
final var dateTime2 = LocalDateTime.of(2104, 2, 1, 0, 0);
System.out.println(dateTime2); // 2104-02-01T00:00
System.out.println(dateTime2.range(ChronoField.DAY_OF_MONTH)); // 1 - 29
LocalDate toLocalDate ()
final var dateTime = LocalDateTime.of(2100, 4, 14, 12, 30, 45);
System.out.println(dateTime); // 2100-04-14T12:30:45
System.out.println(dateTime.toLocalDate()); // 2100-04-14
final var dateTime = LocalDateTime.of(1900, 1, 1, 0, 0);
System.out.println(dateTime); // 1900-01-01T00:00
System.out.println(dateTime.toLocalDate()); // 1900-01-01
final var dateTime = LocalDateTime.MAX;
System.out.println(dateTime); // +999999999-12-31T23:59:59.999999999
System.out.println(dateTime.toLocalDate()); // +999999999-12-31
final var dateTime = LocalDateTime.MIN;
System.out.println(dateTime); // -999999999-01-01T00:00
System.out.println(dateTime.toLocalDate()); // -999999999-01-01
LocalTime toLocalTime ()
final var dateTime = LocalDateTime.of(2100, 4, 14, 12, 30, 45);
System.out.println(dateTime); // 2100-04-14T12:30:45
System.out.println(dateTime.toLocalTime()); // 12:30:45
final var dateTime = LocalDateTime.of(1900, 1, 1, 0, 0);
System.out.println(dateTime); // 1900-01-01T00:00
System.out.println(dateTime.toLocalTime()); // 00:00
final var dateTime = LocalDateTime.MAX;
System.out.println(dateTime); // +999999999-12-31T23:59:59.999999999
System.out.println(dateTime.toLocalTime()); // 23:59:59.999999999
final var dateTime = LocalDateTime.MIN;
System.out.println(dateTime); // -999999999-01-01T00:00
System.out.println(dateTime.toLocalTime()); // 00:00
String toString ()
final var dateTime = LocalDateTime.of(2100, 4, 14, 12, 30, 45);
final var str = dateTime.toString();
System.out.println(str); // 2100-04-14T12:30:45
final var dateTime = LocalDateTime.of(2100, 12, 1, 23, 59);
final var str = dateTime.toString();
System.out.println(str); // 2100-12-01T23:59
final var dateTime1 = LocalDateTime.of(1900, 1, 1, 0, 0, 0, 123000000);
final var str1 = dateTime1.toString();
System.out.println(str1); // 1900-01-01T00:00:00.123
final var dateTime2 = LocalDateTime.of(1900, 1, 1, 0, 0, 0, 123000);
final var str2 = dateTime2.toString();
System.out.println(str2); // 1900-01-01T00:00:00.000123
final var dateTime3 = LocalDateTime.of(1900, 1, 1, 0, 0, 0, 123);
final var str3 = dateTime3.toString();
System.out.println(str3); // 1900-01-01T00:00:00.000000123
final var dateTime = LocalDateTime.MAX;
final var str = dateTime.toString();
System.out.println(str); // +999999999-12-31T23:59:59.999999999
final var dateTime = LocalDateTime.MIN;
final var str = dateTime.toString();
System.out.println(str); // -999999999-01-01T00:00
LocalDateTime truncatedTo (TemporalUnit unit)
final var dateTime = LocalDateTime.of(2100, 4, 14, 12, 30, 15, 123456789);
System.out.println(dateTime); // 2100-04-14T12:30:15.123456789
System.out.println(dateTime.truncatedTo(ChronoUnit.DAYS)); // 2100-04-14T00:00
System.out.println(dateTime.truncatedTo(ChronoUnit.HOURS)); // 2100-04-14T12:00
System.out.println(dateTime.truncatedTo(ChronoUnit.MINUTES)); // 2100-04-14T12:30
System.out.println(dateTime.truncatedTo(ChronoUnit.SECONDS)); // 2100-04-14T12:30:15
final var ret = dateTime.truncatedTo(ChronoUnit.NANOS);
System.out.println(ret); // 2100-04-14T12:30:15.123456789
long until (Temporal endExclusive, TemporalUnit unit)
final var dateTime1 = LocalDateTime.of(2100, 1, 1, 0, 0);
final var dateTime2 = LocalDateTime.of(2101, 2, 2, 0, 0);
System.out.println(dateTime1); // 2100-01-01T00:00
System.out.println(dateTime2); // 2101-02-02T00:00
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 = LocalDateTime.of(2100, 1, 1, 1, 2);
final var dateTime2 = LocalDateTime.of(2100, 1, 1, 3, 32, 15, 123);
System.out.println(dateTime1); // 2100-01-01T01:02
System.out.println(dateTime2); // 2100-01-01T03:32:15.000000123
System.out.println(dateTime1.until(dateTime2, ChronoUnit.YEARS)); // 0
System.out.println(dateTime1.until(dateTime2, ChronoUnit.MONTHS)); // 0
System.out.println(dateTime1.until(dateTime2, ChronoUnit.DAYS)); // 0
System.out.println(dateTime1.until(dateTime2, ChronoUnit.HOURS)); // 2
System.out.println(dateTime1.until(dateTime2, ChronoUnit.MINUTES)); // 150
System.out.println(dateTime1.until(dateTime2, ChronoUnit.SECONDS)); // 9015
System.out.println(dateTime1.until(dateTime2, ChronoUnit.NANOS)); // 9015000000123
final var dateTime1 = LocalDateTime.of(1900, 8, 14, 0, 0);
final var dateTime2 = LocalDateTime.of(1900, 6, 15, 0, 0);
System.out.println(dateTime1); // 1900-08-14T00:00
System.out.println(dateTime2); // 1900-06-15T00:00
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
LocalDateTime with (TemporalAdjuster adjuster)
final var dateTime = LocalDateTime.of(2100, 3, 15, 0, 0);
System.out.println(dateTime); // 2100-03-15T00:00
final var ret1 = dateTime.with(TemporalAdjusters.firstDayOfMonth());
System.out.println(ret1); // 2100-03-01T00:00
final var ret2 = dateTime.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(ret2); // 2100-03-31T00:00
final var dateTime = LocalDateTime.of(2100, 4, 14, 12, 30);
System.out.println(dateTime); // 2100-04-14T12:30
System.out.println(dateTime.with(Year.of(1999))); // 1999-04-14T12:30
System.out.println(dateTime.with(Month.JANUARY)); // 2100-01-14T12:30
System.out.println(dateTime.with(LocalTime.MAX)); // 2100-04-14T23:59:59.999999999
System.out.println(dateTime.with(LocalDate.MIN)); // -999999999-01-01T12:30
LocalDateTime with (TemporalField field, long newValue)
final var dateTime = LocalDateTime.of(2100, 1, 2, 12, 30, 45, 123);
System.out.println(dateTime); // 2100-01-02T12:30:45.000000123
final var ret1 = dateTime.with(ChronoField.YEAR, 1999);
System.out.println(ret1); // 1999-01-02T12:30:45.000000123
final var ret2 = dateTime.with(ChronoField.MONTH_OF_YEAR, 12);
System.out.println(ret2); // 2100-12-02T12:30:45.000000123
final var ret3 = dateTime.with(ChronoField.DAY_OF_MONTH, 30);
System.out.println(ret3); // 2100-01-30T12:30:45.000000123
final var ret4 = dateTime.with(ChronoField.HOUR_OF_DAY, 23);
System.out.println(ret4); // 2100-01-02T23:30:45.000000123
final var ret5 = dateTime.with(ChronoField.MINUTE_OF_HOUR, 59);
System.out.println(ret5); // 2100-01-02T12:59:45.000000123
final var ret6 = dateTime.with(ChronoField.SECOND_OF_MINUTE, 0);
System.out.println(ret6); // 2100-01-02T12:30:00.000000123
final var ret7 = dateTime.with(ChronoField.NANO_OF_SECOND, 456);
System.out.println(ret7); // 2100-01-02T12:30:45.000000456
LocalDateTime withDayOfMonth (int dayOfMonth)
final var dateTime = LocalDateTime.of(2100, 4, 8, 0, 0);
System.out.println(dateTime); // 2100-04-08T00:00
System.out.println(dateTime.withDayOfMonth(1)); // 2100-04-01T00:00
System.out.println(dateTime.withDayOfMonth(2)); // 2100-04-02T00:00
System.out.println(dateTime.withDayOfMonth(29)); // 2100-04-29T00:00
System.out.println(dateTime.withDayOfMonth(30)); // 2100-04-30T00:00
LocalDateTime withDayOfYear (int dayOfYear)
final var dateTime = LocalDateTime.of(2100, 4, 8, 12, 30);
System.out.println(dateTime); // 2100-04-08T12:30
System.out.println(dateTime.withDayOfYear(1)); // 2100-01-01T12:30
System.out.println(dateTime.withDayOfYear(2)); // 2100-01-02T12:30
System.out.println(dateTime.withDayOfYear(30)); // 2100-01-30T12:30
System.out.println(dateTime.withDayOfYear(31)); // 2100-01-31T12:30
System.out.println(dateTime.withDayOfYear(32)); // 2100-02-01T12:30
System.out.println(dateTime.withDayOfYear(33)); // 2100-02-02T12:30
System.out.println(dateTime.withDayOfYear(365)); // 2100-12-31T12:30
LocalDateTime withHour (int hour)
final var dateTime = LocalDateTime.of(2100, 1, 1, 12, 30);
System.out.println(dateTime); // 2100-01-01T12:30
System.out.println(dateTime.withHour(0)); // 2100-01-01T00:30
System.out.println(dateTime.withHour(23)); // 2100-01-01T23:30
LocalDateTime withMinute (int minute)
final var dateTime = LocalDateTime.of(2100, 1, 1, 12, 30);
System.out.println(dateTime); // 2100-01-01T12:30
System.out.println(dateTime.withMinute(0)); // 2100-01-01T12:00
System.out.println(dateTime.withMinute(59)); // 2100-01-01T12:59
LocalDateTime withMonth (int month)
final var dateTime = LocalDateTime.of(2100, 4, 8, 12, 30);
System.out.println(dateTime); // 2100-04-08T12:30
System.out.println(dateTime.withMonth(1)); // 2100-01-08T12:30
System.out.println(dateTime.withMonth(2)); // 2100-02-08T12:30
System.out.println(dateTime.withMonth(11)); // 2100-11-08T12:30
System.out.println(dateTime.withMonth(12)); // 2100-12-08T12:30
LocalDateTime withNano (int nanoOfSecond)
final var dateTime = LocalDateTime.of(2100, 1, 1, 0, 0, 0, 123);
System.out.println(dateTime); // 2100-01-01T00:00:00.000000123
System.out.println(dateTime.withNano(0)); // 2100-01-01T00:00
System.out.println(dateTime.withNano(999999999)); // 2100-01-01T00:00:00.999999999
LocalDateTime withSecond (int second)
final var dateTime = LocalDateTime.of(2100, 1, 1, 12, 30, 45);
System.out.println(dateTime); // 2100-01-01T12:30:45
System.out.println(dateTime.withSecond(0)); // 2100-01-01T12:30
System.out.println(dateTime.withSecond(1)); // 2100-01-01T12:30:01
System.out.println(dateTime.withSecond(59)); // 2100-01-01T12:30:59
LocalDateTime withYear (int year)
final var dateTime = LocalDateTime.of(2100, 4, 8, 12, 30);
System.out.println(dateTime); // 2100-04-08T12:30
System.out.println(dateTime.withYear(1999)); // 1999-04-08T12:30
System.out.println(dateTime.withYear(-999)); // -0999-04-08T12:30
System.out.println(dateTime.withYear(2500)); // 2500-04-08T12:30
ChronoLocalDateTimeで宣言されたメソッド
getChronology, toEpochSecond, toInstant
「Java API 使用例 : ChronoLocalDateTime」をご参照ください。
関連記事
- 日付・時刻の基本
- Date, CalendarではなくLocalDateTime, ZonedDateTimeを使おう
- 文字列と日付・時刻の変換
- 日付と時刻、曜日の計算
- 現在時刻(日時)の取得いろいろ
- 現在の曜日(DayOfWeek)を取得
- ZoneIdとZoneOffsetの違い
- API 使用例
- Calendar (カレンダー)
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock (時計)
- Date (日付・時刻)
- DateTimeException (日付・時刻の例外)
- DateTimeParseException (日付・時刻の解析例外)
- DayOfWeek (曜日)
- Duration (時間の量)
- Era (紀元)
- Instant (時点)
- InstantSource
- JapaneseDate (和暦を使った日付)
- LocalDate (日付・タイムゾーンなし)
- LocalTime (時刻・タイムゾーンなし)
- Month (月)
- MonthDay (月・日)
- OffsetDateTime (日時・オフセットあり)
- OffsetTime (時刻・オフセットあり)
- Period (日付の量)
- Temporal
- TemporalAccessor
- TemporalAdjuster (日付・時刻の調整)
- TemporalAdjusters (日付・時刻の調整ユーティリティ)
- TimeZone (タイムゾーン)
- Year (年)
- YearMonth (年・月)
- ZonedDateTime (日時・タイムゾーンあり)
- ZoneId (タイムゾーンID)
- ZoneOffset (タイムゾーン・オフセット)