Java : ZonedDateTime con ejemplos
ZonedDateTime (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de ZonedDateTime.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
final var dateTime = ZonedDateTime.of(2100, 4, 14, 13, 30, 15, 123000000, zone);
System.out.println(dateTime); // 2100-04-14T13:30:15.123-07:00[America/Los_Angeles]
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.getZone()); // America/Los_Angeles
System.out.println(dateTime.getOffset()); // -07:00
Methods
boolean equals (Object obj)
final var dateTime1 = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime2 = ZonedDateTime.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 zone1 = ZoneId.of("America/Los_Angeles");
final var zone2 = ZoneId.of("Asia/Tokyo");
final var dateTime1 = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, zone1);
final var dateTime2 = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, zone2);
System.out.println(dateTime1); // 2100-01-01T00:00-08:00[America/Los_Angeles]
System.out.println(dateTime2); // 2100-01-01T00:00+09:00[Asia/Tokyo]
System.out.println(dateTime1.equals(dateTime2)); // false
final var dateTime1 = ZonedDateTime.of(2100, 4, 14, 15, 20, 0, 0, ZoneOffset.UTC);
final var dateTime2 = ZonedDateTime.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 = ZonedDateTime.of(2100, 4, 15, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime2 = ZonedDateTime.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 = ZonedDateTime.of(2100, 4, 13, 0, 0, 0, 123, ZoneOffset.UTC);
final var dateTime2 = ZonedDateTime.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 zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
final var dateTime = ZonedDateTime.of(2100, 4, 7, 13, 30, 45, 0, zone);
System.out.println(dateTime); // 2100-04-07T13:30:45-07:00[America/Los_Angeles]
{
final var ret1 = dateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
System.out.println(ret1); // 2100-04-07T13:30:45-07:00[America/Los_Angeles]
final var ret2 = dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
System.out.println(ret2); // 2100-04-07T13:30:45-07:00
final var ret3 = dateTime.format(DateTimeFormatter.ofPattern("MM/dd/yyyy, HH:mm:ss z"));
System.out.println(ret3); // 04/07/2100, 13:30:45 PDT
}
{
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var ret1 = dateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL));
System.out.println(ret1); // Wednesday, April 7, 2100, 1:30:45 PM Pacific Daylight Time
final var ret2 = dateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG));
System.out.println(ret2); // April 7, 2100, 1:30:45 PM PDT
final var ret3 = dateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
System.out.println(ret3); // Apr 7, 2100, 1:30:45 PM
final var ret4 = dateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT));
System.out.println(ret4); // 4/7/00, 1:30 PM
}
static ZonedDateTime from (TemporalAccessor temporal)
final var offset = ZoneOffset.ofHours(-8);
System.out.println(offset); // -08:00
final var temporal = OffsetDateTime.of(2100, 1, 2, 12, 30, 0, 0, offset);
System.out.println(temporal); // 2100-01-02T12:30-08:00
System.out.println(ZonedDateTime.from(temporal)); // 2100-01-02T12:30-08:00
int get (TemporalField field)
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var dateTime = ZonedDateTime.of(2100, 4, 13, 12, 30, 45, 123, zone);
System.out.println(dateTime); // 2100-04-13T12:30:45.000000123-01:00[Etc/GMT+1]
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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.of(2100, 1, 2, 3, 4, 5, 123000000, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-02T03:04:05.123Z
System.out.println(dateTime.getHour()); // 3
final var offset = ZoneOffset.ofHours(1);
System.out.println(offset); // +01:00
final var dateTime = ZonedDateTime.of(2100, 1, 1, 14, 30, 15, 0, offset);
System.out.println(dateTime); // 2100-01-01T14:30:15+01:00
System.out.println(dateTime.getHour()); // 14
long getLong (TemporalField field)
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var dateTime = ZonedDateTime.of(2100, 4, 13, 12, 30, 45, 123, zone);
System.out.println(dateTime); // 2100-04-13T12:30:45.000000123-01:00[Etc/GMT+1]
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 = ZonedDateTime.of(2100, 5, 1, 0, 45, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-05-01T00:45Z
System.out.println(dateTime.getMinute()); // 45
Month getMonth ()
final var dateTime = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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 dateTime = ZonedDateTime.of(2100, 1, 1, 13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:15Z
System.out.println(dateTime.getNano()); // 0
final var dateTime = ZonedDateTime.of(
2100, 1, 1, 13, 30, 15, 123456789, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:15.123456789Z
System.out.println(dateTime.getNano()); // 123456789
ZoneOffset getOffset ()
final var dateTime = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
System.out.println(dateTime.getOffset()); // Z
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
final var dateTime = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, zone);
System.out.println(dateTime); // 2100-01-01T00:00-08:00[America/Los_Angeles]
System.out.println(dateTime.getOffset()); // -08:00
final var offset = ZoneOffset.ofHoursMinutes(4, 30);
System.out.println(offset); // +04:30
final var dateTime = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, offset);
System.out.println(dateTime); // 2100-01-01T00:00+04:30
System.out.println(dateTime.getOffset()); // +04:30
int getSecond ()
final var dateTime = ZonedDateTime.of(2100, 1, 1, 13, 30, 45, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:45Z
System.out.println(dateTime.getSecond()); // 45
int getYear ()
final var dateTime = ZonedDateTime.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 = ZonedDateTime.of(-3000, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // -3000-01-01T00:00Z
System.out.println(dateTime.getYear()); // -3000
ZoneId getZone ()
final var dateTime = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
System.out.println(dateTime.getZone()); // Z
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
final var dateTime = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, zone);
System.out.println(dateTime); // 2100-01-01T00:00-08:00[America/Los_Angeles]
System.out.println(dateTime.getZone()); // America/Los_Angeles
final var offset = ZoneOffset.ofHoursMinutes(4, 30);
System.out.println(offset); // +04:30
final var dateTime = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, offset);
System.out.println(dateTime); // 2100-01-01T00:00+04:30
System.out.println(dateTime.getZone()); // +04:30
int hashCode ()
final var dateTime1 = ZonedDateTime.of(2100, 4, 13, 12, 30, 40, 123, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-04-13T12:30:40.000000123Z
System.out.println(dateTime1.hashCode()); // -1317871232
final var dateTime2 = ZonedDateTime.of(1800, 1, 1, 1, 0, 0, 0, ZoneOffset.ofHours(1));
System.out.println(dateTime2); // 1800-01-01T01:00+01:00
System.out.println(dateTime2.hashCode()); // 813735319
boolean isSupported (TemporalField field)
final var dateTime = ZonedDateTime.of(2100, 4, 13, 12, 30, 45, 123, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-13T12:30:45.000000123Z
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 = ZonedDateTime.of(2100, 4, 13, 12, 30, 45, 123, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-13T12:30:45.000000123Z
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
ZonedDateTime minus (long amountToSubtract, TemporalUnit unit)
final var dateTime = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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
final var offset = ZoneOffset.ofHours(1);
System.out.println(offset); // +01:00
final var dateTime = ZonedDateTime.of(2100, 1, 1, 10, 20, 30, 0, offset);
System.out.println(dateTime); // 2100-01-01T10:20:30+01:00
final var ret1 = dateTime.minus(1, ChronoUnit.HOURS);
System.out.println(ret1); // 2100-01-01T09:20:30+01:00
final var ret2 = dateTime.minus(2, ChronoUnit.MINUTES);
System.out.println(ret2); // 2100-01-01T10:18:30+01:00
final var ret3 = dateTime.minus(3, ChronoUnit.SECONDS);
System.out.println(ret3); // 2100-01-01T10:20:27+01:00
// An example of a time-zone offset change.
final var zone = ZoneId.of("Europe/Paris");
System.out.println(zone); // Europe/Paris
final var dateTime = ZonedDateTime.of(2100, 11, 1, 3, 30, 0, 0, zone);
System.out.println(dateTime); // 2100-11-01T03:30+01:00[Europe/Paris]
final var ret1 = dateTime.minus(1, ChronoUnit.DAYS);
System.out.println(ret1); // 2100-10-31T03:30+01:00[Europe/Paris]
final var ret2 = dateTime.minus(2, ChronoUnit.DAYS);
System.out.println(ret2); // 2100-10-30T03:30+02:00[Europe/Paris]
ZonedDateTime minus (TemporalAmount amountToSubtract)
final var dateTime = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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
final var offset = ZoneOffset.ofHours(1);
System.out.println(offset); // +01:00
final var dateTime = ZonedDateTime.of(2100, 1, 1, 10, 20, 30, 0, offset);
System.out.println(dateTime); // 2100-01-01T10:20:30+01:00
final var ret1 = dateTime.minus(Duration.ofHours(1));
System.out.println(ret1); // 2100-01-01T09:20:30+01:00
final var ret2 = dateTime.minus(Duration.ofMinutes(2));
System.out.println(ret2); // 2100-01-01T10:18:30+01:00
final var ret3 = dateTime.minus(Duration.ofSeconds(3));
System.out.println(ret3); // 2100-01-01T10:20:27+01:00
// An example of a time-zone offset change.
final var zone = ZoneId.of("Europe/Paris");
System.out.println(zone); // Europe/Paris
final var dateTime = ZonedDateTime.of(2100, 11, 1, 3, 30, 0, 0, zone);
System.out.println(dateTime); // 2100-11-01T03:30+01:00[Europe/Paris]
final var ret1 = dateTime.minus(Period.ofDays(1));
System.out.println(ret1); // 2100-10-31T03:30+01:00[Europe/Paris]
final var ret2 = dateTime.minus(Period.ofDays(2));
System.out.println(ret2); // 2100-10-30T03:30+02:00[Europe/Paris]
ZonedDateTime minusDays (long days)
final var dateTime = ZonedDateTime.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
// An example of a time-zone offset change.
final var zone = ZoneId.of("Europe/Paris");
System.out.println(zone); // Europe/Paris
final var dateTime = ZonedDateTime.of(2100, 11, 1, 3, 30, 0, 0, zone);
System.out.println(dateTime); // 2100-11-01T03:30+01:00[Europe/Paris]
System.out.println(dateTime.minusDays(1)); // 2100-10-31T03:30+01:00[Europe/Paris]
System.out.println(dateTime.minusDays(2)); // 2100-10-30T03:30+02:00[Europe/Paris]
ZonedDateTime minusHours (long hours)
final var dateTime = ZonedDateTime.of(2100, 1, 1, 13, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30Z
System.out.println(dateTime.minusHours(1)); // 2100-01-01T12:30Z
System.out.println(dateTime.minusHours(2)); // 2100-01-01T11:30Z
System.out.println(dateTime.minusHours(13)); // 2100-01-01T00:30Z
System.out.println(dateTime.minusHours(14)); // 2099-12-31T23:30Z
// An example of a time-zone offset change.
final var zone = ZoneId.of("Europe/Paris");
System.out.println(zone); // Europe/Paris
final var dateTime = ZonedDateTime.of(2100, 10, 31, 3, 30, 0, 0, zone);
System.out.println(dateTime); // 2100-10-31T03:30+01:00[Europe/Paris]
System.out.println(dateTime.minusHours(1)); // 2100-10-31T02:30+01:00[Europe/Paris]
System.out.println(dateTime.minusHours(2)); // 2100-10-31T02:30+02:00[Europe/Paris]
System.out.println(dateTime.minusHours(3)); // 2100-10-31T01:30+02:00[Europe/Paris]
ZonedDateTime minusMinutes (long minutes)
final var dateTime = ZonedDateTime.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
ZonedDateTime minusMonths (long months)
final var dateTime = ZonedDateTime.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 = ZonedDateTime.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
// An example of a time-zone offset change.
final var zone = ZoneId.of("Europe/Paris");
System.out.println(zone); // Europe/Paris
final var dateTime = ZonedDateTime.of(2100, 12, 1, 3, 30, 0, 0, zone);
System.out.println(dateTime); // 2100-12-01T03:30+01:00[Europe/Paris]
System.out.println(dateTime.minusMonths(1)); // 2100-11-01T03:30+01:00[Europe/Paris]
System.out.println(dateTime.minusMonths(2)); // 2100-10-01T03:30+02:00[Europe/Paris]
ZonedDateTime minusNanos (long nanos)
final var dateTime = ZonedDateTime.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
System.out.println(dateTime.minusNanos(1000000000L * 60 * 60 * 24 * 365)); // 2099-01-01T13:30:15.000000123Z
ZonedDateTime minusSeconds (long seconds)
final var dateTime = ZonedDateTime.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
System.out.println(dateTime.minusSeconds(60 * 60 * 24 * 365)); // 2099-01-01T13:30:15Z
ZonedDateTime minusWeeks (long weeks)
final var dateTime = ZonedDateTime.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
// An example of a time-zone offset change.
final var zone = ZoneId.of("Europe/Paris");
System.out.println(zone); // Europe/Paris
final var dateTime = ZonedDateTime.of(2100, 11, 7, 3, 30, 0, 0, zone);
System.out.println(dateTime); // 2100-11-07T03:30+01:00[Europe/Paris]
System.out.println(dateTime.minusWeeks(1)); // 2100-10-31T03:30+01:00[Europe/Paris]
System.out.println(dateTime.minusWeeks(2)); // 2100-10-24T03:30+02:00[Europe/Paris]
ZonedDateTime minusYears (long years)
final var dateTime = ZonedDateTime.of(2101, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2101-05-20T00:00Z
System.out.println(dateTime.minusYears(1)); // 2100-05-20T00:00Z
System.out.println(dateTime.minusYears(2)); // 2099-05-20T00:00Z
System.out.println(dateTime.minusYears(10)); // 2091-05-20T00:00Z
final var dateTime = ZonedDateTime.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 ZonedDateTime now ()
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
final var dateTime = ZonedDateTime.now();
System.out.println(dateTime); // 2024-05-30T00:47:22.294467400-07:00[America/Los_Angeles]
static ZonedDateTime now (Clock clock)
// A clock advanced 5 days.
final var clock = Clock.offset(Clock.systemDefaultZone(), Duration.ofDays(5));
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
final var dateTime1 = ZonedDateTime.now();
System.out.println(dateTime1); // 2024-05-30T00:49:51.134199300-07:00[America/Los_Angeles]
final var dateTime2 = ZonedDateTime.now(clock);
System.out.println(dateTime2); // 2024-06-04T00:49:51.135198900-07:00[America/Los_Angeles]
static ZonedDateTime now (ZoneId zone)
final var zone1 = ZoneOffset.UTC;
System.out.println(zone1); // Z
final var zone2 = ZoneId.of("Europe/Paris");
System.out.println(zone2); // Europe/Paris
final var dateTime1 = ZonedDateTime.now(zone1);
System.out.println(dateTime1); // 2024-05-30T07:52:36.780017800Z
final var dateTime2 = ZonedDateTime.now(zone2);
System.out.println(dateTime2); // 2024-05-30T09:52:36.780017800+02:00[Europe/Paris]
static ZonedDateTime of (int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone)
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
final var ret = ZonedDateTime.of(2100, 2, 28, 0, 0, 0, 0, zone);
System.out.println(ret); // 2100-02-28T00:00-08:00[America/Los_Angeles]
final var ret1 = ZonedDateTime.of(2100, 4, 14, 13, 30, 10, 123, ZoneOffset.UTC);
System.out.println(ret1); // 2100-04-14T13:30:10.000000123Z
final var ret2 = ZonedDateTime.of(-999, 12, 1, 23, 59, 59, 999999999, ZoneOffset.UTC);
System.out.println(ret2); // -0999-12-01T23:59:59.999999999Z
static ZonedDateTime of (LocalDate date, LocalTime time, ZoneId zone)
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
final var ret1 = ZonedDateTime.of(date, time, ZoneOffset.UTC);
System.out.println(ret1); // 2100-04-14T13:30:10Z
final var ret2 = ZonedDateTime.of(date, time, ZoneId.systemDefault());
System.out.println(ret2); // 2100-04-14T13:30:10-07:00[America/Los_Angeles]
final var date = LocalDate.of(2100, Month.FEBRUARY, 28);
System.out.println(date); // 2100-02-28
final var ret = ZonedDateTime.of(date, LocalTime.MIN, ZoneOffset.UTC);
System.out.println(ret); // 2100-02-28T00:00Z
static ZonedDateTime of (LocalDateTime localDateTime, ZoneId zone)
final var dateTime = LocalDateTime.of(2100, 4, 14, 13, 30);
System.out.println(dateTime); // 2100-04-14T13:30
final var ret1 = ZonedDateTime.of(dateTime, ZoneOffset.UTC);
System.out.println(ret1); // 2100-04-14T13:30Z
final var ret2 = ZonedDateTime.of(dateTime, ZoneId.systemDefault());
System.out.println(ret2); // 2100-04-14T13:30-07:00[America/Los_Angeles]
final var ret1 = ZonedDateTime.of(LocalDateTime.MAX, ZoneOffset.UTC);
System.out.println(ret1); // +999999999-12-31T23:59:59.999999999Z
final var ret2 = ZonedDateTime.of(LocalDateTime.MIN, ZoneOffset.UTC);
System.out.println(ret2); // -999999999-01-01T00:00Z
static ZonedDateTime ofInstant (Instant instant, ZoneId zone)
final var instant = Instant.ofEpochSecond(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z
final var dateTime1 = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-01-01T00:00Z
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
final var dateTime2 = ZonedDateTime.ofInstant(instant, zone);
System.out.println(dateTime2); // 2099-12-31T16:00-08:00[America/Los_Angeles]
static ZonedDateTime ofInstant (LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone)
final var dateTime = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00
final var ret = ZonedDateTime.ofInstant(dateTime, ZoneOffset.ofHours(1), ZoneOffset.UTC);
System.out.println(ret); // 2099-12-31T23:00Z
static ZonedDateTime ofLocal (LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset)
// An example of a time-zone offset change.
final var localDateTime = LocalDateTime.of(2100, 10, 31, 2, 30);
System.out.println(localDateTime); // 2100-10-31T02:30
final var zone = ZoneId.of("Europe/Paris");
System.out.println(zone); // Europe/Paris
final var ret1 = ZonedDateTime.ofLocal(localDateTime, zone, ZoneOffset.ofHours(1));
System.out.println(ret1); // 2100-10-31T02:30+01:00[Europe/Paris]
final var ret2 = ZonedDateTime.ofLocal(localDateTime, zone, ZoneOffset.ofHours(2));
System.out.println(ret2); // 2100-10-31T02:30+02:00[Europe/Paris]
static ZonedDateTime ofStrict (LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone)
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var localDateTime = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(localDateTime); // 2100-01-01T00:00
final var ret1 = ZonedDateTime.ofStrict(localDateTime, ZoneOffset.ofHours(-1), zone);
System.out.println(ret1); // 2100-01-01T00:00-01:00[Etc/GMT+1]
try {
final var ret2 = ZonedDateTime.ofStrict(localDateTime, ZoneOffset.ofHours(-2), zone);
} catch (DateTimeException e) {
System.out.println("DateTimeException! : " + e.getMessage());
}
// Result
// ↓
//DateTimeException! : ZoneOffset '-02:00' is not valid for
// LocalDateTime '2100-01-01T00:00' in zone 'Etc/GMT+1'
static ZonedDateTime parse (CharSequence text)
final var ret1 = ZonedDateTime.parse("2100-04-14T00:00Z");
System.out.println(ret1); // 2100-04-14T00:00Z
final var ret2 = ZonedDateTime.parse("2100-04-14T13:30:45Z");
System.out.println(ret2); // 2100-04-14T13:30:45Z
final var ret3 = ZonedDateTime.parse("2100-04-14T13:30:45.123456789Z");
System.out.println(ret3); // 2100-04-14T13:30:45.123456789Z
final var ret4 = ZonedDateTime.parse("2100-10-25T00:00-07:00[America/Los_Angeles]");
System.out.println(ret4); // 2100-10-25T00:00-07:00[America/Los_Angeles]
try {
final var ret = ZonedDateTime.parse("2100-04-14T00:00");
} catch (DateTimeParseException e) {
System.out.println("DateTimeParseException! : " + e.getMessage());
}
// Result
// ↓
//DateTimeParseException! : Text '2100-04-14T00:00' could not be parsed at index 16
static ZonedDateTime parse (CharSequence text, DateTimeFormatter formatter)
final var ret1 = ZonedDateTime.parse(
"2100-04-14T13:30:45.123456789Z",
DateTimeFormatter.ISO_ZONED_DATE_TIME);
System.out.println(ret1); // 2100-04-14T13:30:45.123456789Z
final var ret2 = ZonedDateTime.parse(
"2100-04-14T00:00-07:00[America/Los_Angeles]",
DateTimeFormatter.ISO_ZONED_DATE_TIME);
System.out.println(ret2); // 2100-04-14T00:00-07:00[America/Los_Angeles]
final var ret3 = ZonedDateTime.parse(
"2100-04-30T12:30-05:00",
DateTimeFormatter.ISO_ZONED_DATE_TIME);
System.out.println(ret3); // 2100-04-30T12:30-05:00
final var ret = ZonedDateTime.parse(
"4/7/2100, 13:30:45 PDT",
DateTimeFormatter.ofPattern("M/d/y, H:m:s z"));
System.out.println(ret); // 2100-04-07T13:30:45-07:00[America/Los_Angeles]
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
final var dateTime = ZonedDateTime.of(2100, 4, 7, 1, 30, 45, 0, zone);
System.out.println(dateTime); // 2100-04-07T01:30:45-07:00[America/Los_Angeles]
final var formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
final var formatted = dateTime.format(formatter);
System.out.println(formatted); // April 7, 2100, 1:30:45 AM PDT
final var ret = ZonedDateTime.parse(formatted, formatter);
System.out.println(ret); // 2100-04-07T01:30:45-07:00[America/Los_Angeles]
ZonedDateTime plus (long amountToAdd, TemporalUnit unit)
final var dateTime = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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
final var offset = ZoneOffset.ofHours(1);
System.out.println(offset); // +01:00
final var dateTime = ZonedDateTime.of(2100, 1, 1, 10, 20, 30, 0, offset);
System.out.println(dateTime); // 2100-01-01T10:20:30+01:00
final var ret1 = dateTime.plus(1, ChronoUnit.HOURS);
System.out.println(ret1); // 2100-01-01T11:20:30+01:00
final var ret2 = dateTime.plus(2, ChronoUnit.MINUTES);
System.out.println(ret2); // 2100-01-01T10:22:30+01:00
final var ret3 = dateTime.plus(3, ChronoUnit.SECONDS);
System.out.println(ret3); // 2100-01-01T10:20:33+01:00
// An example of a time-zone offset change.
final var zone = ZoneId.of("Europe/Paris");
System.out.println(zone); // Europe/Paris
final var dateTime = ZonedDateTime.of(2100, 10, 29, 3, 30, 0, 0, zone);
System.out.println(dateTime); // 2100-10-29T03:30+02:00[Europe/Paris]
final var ret1 = dateTime.plus(1, ChronoUnit.DAYS);
System.out.println(ret1); // 2100-10-30T03:30+02:00[Europe/Paris]
final var ret2 = dateTime.plus(2, ChronoUnit.DAYS);
System.out.println(ret2); // 2100-10-31T03:30+01:00[Europe/Paris]
ZonedDateTime plus (TemporalAmount amountToAdd)
final var dateTime = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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
final var offset = ZoneOffset.ofHours(1);
System.out.println(offset); // +01:00
final var dateTime = ZonedDateTime.of(2100, 1, 1, 10, 20, 30, 0, offset);
System.out.println(dateTime); // 2100-01-01T10:20:30+01:00
final var ret1 = dateTime.plus(Duration.ofHours(1));
System.out.println(ret1); // 2100-01-01T11:20:30+01:00
final var ret2 = dateTime.plus(Duration.ofMinutes(2));
System.out.println(ret2); // 2100-01-01T10:22:30+01:00
final var ret3 = dateTime.plus(Duration.ofSeconds(3));
System.out.println(ret3); // 2100-01-01T10:20:33+01:00
// An example of a time-zone offset change.
final var zone = ZoneId.of("Europe/Paris");
System.out.println(zone); // Europe/Paris
final var dateTime = ZonedDateTime.of(2100, 10, 29, 3, 30, 0, 0, zone);
System.out.println(dateTime); // 2100-10-29T03:30+02:00[Europe/Paris]
final var ret1 = dateTime.plus(Period.ofDays(1));
System.out.println(ret1); // 2100-10-30T03:30+02:00[Europe/Paris]
final var ret2 = dateTime.plus(Period.ofDays(2));
System.out.println(ret2); // 2100-10-31T03:30+01:00[Europe/Paris]
ZonedDateTime plusDays (long days)
final var dateTime = ZonedDateTime.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 = ZonedDateTime.of(2099, 12, 31, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2099-12-31T00:00Z
System.out.println(dateTime.plusDays(1)); // 2100-01-01T00:00Z
// An example of a time-zone offset change.
final var zone = ZoneId.of("Europe/Paris");
System.out.println(zone); // Europe/Paris
final var dateTime = ZonedDateTime.of(2100, 10, 29, 3, 30, 0, 0, zone);
System.out.println(dateTime); // 2100-10-29T03:30+02:00[Europe/Paris]
System.out.println(dateTime.plusDays(1)); // 2100-10-30T03:30+02:00[Europe/Paris]
System.out.println(dateTime.plusDays(2)); // 2100-10-31T03:30+01:00[Europe/Paris]
ZonedDateTime plusHours (long hours)
final var dateTime = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
System.out.println(dateTime.plusHours(1)); // 2100-01-01T01:00Z
System.out.println(dateTime.plusHours(2)); // 2100-01-01T02:00Z
System.out.println(dateTime.plusHours(3)); // 2100-01-01T03:00Z
System.out.println(dateTime.plusHours(24)); // 2100-01-02T00:00Z
System.out.println(dateTime.plusHours(25)); // 2100-01-02T01:00Z
// An example of a time-zone offset change.
final var zone = ZoneId.of("Europe/Paris");
System.out.println(zone); // Europe/Paris
final var dateTime = ZonedDateTime.of(2100, 10, 31, 0, 30, 0, 0, zone);
System.out.println(dateTime); // 2100-10-31T00:30+02:00[Europe/Paris]
System.out.println(dateTime.plusHours(1)); // 2100-10-31T01:30+02:00[Europe/Paris]
System.out.println(dateTime.plusHours(2)); // 2100-10-31T02:30+02:00[Europe/Paris]
System.out.println(dateTime.plusHours(3)); // 2100-10-31T02:30+01:00[Europe/Paris]
ZonedDateTime plusMinutes (long minutes)
final var dateTime = ZonedDateTime.of(2099, 1, 1, 13, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2099-01-01T13:30Z
System.out.println(dateTime.plusMinutes(1)); // 2099-01-01T13:31Z
System.out.println(dateTime.plusMinutes(2)); // 2099-01-01T13:32Z
System.out.println(dateTime.plusMinutes(30)); // 2099-01-01T14:00Z
System.out.println(dateTime.plusMinutes(31)); // 2099-01-01T14:01Z
System.out.println(dateTime.plusMinutes(60 * 24)); // 2099-01-02T13:30Z
System.out.println(dateTime.plusMinutes(60 * 24 * 365)); // 2100-01-01T13:30Z
ZonedDateTime plusMonths (long months)
final var dateTime = ZonedDateTime.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 = ZonedDateTime.of(2101, 1, 31, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2101-01-31T00:00Z
System.out.println(dateTime.plusMonths(1)); // 2101-02-28T00:00Z
// An example of a time-zone offset change.
final var zone = ZoneId.of("Europe/Paris");
System.out.println(zone); // Europe/Paris
final var dateTime = ZonedDateTime.of(2100, 9, 1, 3, 30, 0, 0, zone);
System.out.println(dateTime); // 2100-09-01T03:30+02:00[Europe/Paris]
System.out.println(dateTime.plusMonths(1)); // 2100-10-01T03:30+02:00[Europe/Paris]
System.out.println(dateTime.plusMonths(2)); // 2100-11-01T03:30+01:00[Europe/Paris]
ZonedDateTime plusNanos (long nanos)
final var dateTime = ZonedDateTime.of(2100, 1, 1, 13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T13:30:15.000000123Z
System.out.println(dateTime.plusNanos(1)); // 2100-01-01T13:30:15.000000124Z
System.out.println(dateTime.plusNanos(2)); // 2100-01-01T13:30:15.000000125Z
System.out.println(dateTime.plusNanos(1000000000L - 124)); // 2100-01-01T13:30:15.999999999Z
System.out.println(dateTime.plusNanos(1000000000L - 123)); // 2100-01-01T13:30:16Z
ZonedDateTime plusSeconds (long seconds)
final var dateTime = ZonedDateTime.of(2099, 1, 1, 13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2099-01-01T13:30:15Z
System.out.println(dateTime.plusSeconds(1)); // 2099-01-01T13:30:16Z
System.out.println(dateTime.plusSeconds(2)); // 2099-01-01T13:30:17Z
System.out.println(dateTime.plusSeconds(45)); // 2099-01-01T13:31Z
System.out.println(dateTime.plusSeconds(46)); // 2099-01-01T13:31:01Z
System.out.println(dateTime.plusSeconds(60 + 45)); // 2099-01-01T13:32Z
System.out.println(dateTime.plusSeconds(60 * 60)); // 2099-01-01T14:30:15Z
System.out.println(dateTime.plusSeconds(60 * 60 * 24)); // 2099-01-02T13:30:15Z
System.out.println(dateTime.plusSeconds(60 * 60 * 24 * 365)); // 2100-01-01T13:30:15Z
ZonedDateTime plusWeeks (long weeks)
final var dateTime = ZonedDateTime.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
// An example of a time-zone offset change.
final var zone = ZoneId.of("Europe/Paris");
System.out.println(zone); // Europe/Paris
final var dateTime = ZonedDateTime.of(2100, 10, 22, 3, 30, 0, 0, zone);
System.out.println(dateTime); // 2100-10-22T03:30+02:00[Europe/Paris]
System.out.println(dateTime.plusWeeks(1)); // 2100-10-29T03:30+02:00[Europe/Paris]
System.out.println(dateTime.plusWeeks(2)); // 2100-11-05T03:30+01:00[Europe/Paris]
ZonedDateTime plusYears (long years)
final var dateTime = ZonedDateTime.of(2101, 5, 20, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2101-05-20T00:00Z
System.out.println(dateTime.plusYears(1)); // 2102-05-20T00:00Z
System.out.println(dateTime.plusYears(2)); // 2103-05-20T00:00Z
System.out.println(dateTime.plusYears(10)); // 2111-05-20T00:00Z
final var dateTime = ZonedDateTime.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 zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var dateTime = ZonedDateTime.of(2100, 4, 14, 12, 30, 0, 0, zone);
System.out.println(dateTime); // 2100-04-14T12:30-01:00[Etc/GMT+1]
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.zoneId())); // Etc/GMT+1
System.out.println(dateTime.query(TemporalQueries.precision())); // Nanos
ValueRange range (TemporalField field)
final var dateTime = ZonedDateTime.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 = ZonedDateTime.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 = ZonedDateTime.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
LocalDate toLocalDate ()
final var dateTime = ZonedDateTime.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
LocalDateTime toLocalDateTime ()
final var dateTime = ZonedDateTime.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
LocalTime toLocalTime ()
final var dateTime = ZonedDateTime.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
OffsetDateTime toOffsetDateTime ()
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var dateTime = ZonedDateTime.of(2100, 1, 1, 23, 59, 0, 0, zone);
System.out.println(dateTime); // 2100-01-01T23:59-01:00[Etc/GMT+1]
System.out.println(dateTime.toOffsetDateTime()); // 2100-01-01T23:59-01:00
final var dateTime = ZonedDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 1800-01-01T00:00Z
System.out.println(dateTime.toOffsetDateTime()); // 1800-01-01T00:00Z
String toString ()
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var dateTime = ZonedDateTime.of(2100, 4, 14, 12, 30, 45, 0, zone);
final var str = dateTime.toString();
System.out.println(str); // 2100-04-14T12:30:45-01:00[Etc/GMT+1]
final var dateTime = ZonedDateTime.of(2100, 12, 1, 23, 59, 0, 0, ZoneOffset.UTC);
final var str = dateTime.toString();
System.out.println(str); // 2100-12-01T23:59Z
final var dateTime1 = ZonedDateTime.of(1800, 1, 1, 0, 0, 0, 123000000, ZoneOffset.UTC);
final var str1 = dateTime1.toString();
System.out.println(str1); // 1800-01-01T00:00:00.123Z
final var dateTime2 = ZonedDateTime.of(1800, 1, 1, 0, 0, 0, 123000, ZoneOffset.UTC);
final var str2 = dateTime2.toString();
System.out.println(str2); // 1800-01-01T00:00:00.000123Z
final var dateTime3 = ZonedDateTime.of(1800, 1, 1, 0, 0, 0, 123, ZoneOffset.UTC);
final var str3 = dateTime3.toString();
System.out.println(str3); // 1800-01-01T00:00:00.000000123Z
ZonedDateTime truncatedTo (TemporalUnit unit)
final var zone = ZoneOffset.UTC;
System.out.println(zone); // Z
final var dateTime = ZonedDateTime.of(2100, 4, 14, 12, 30, 15, 123456789, zone);
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 = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime2 = ZonedDateTime.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 zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var dateTime1 = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, zone);
final var dateTime2 = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-01-01T00:00-01:00[Etc/GMT+1]
System.out.println(dateTime2); // 2100-01-01T00:00Z
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)); // -1
System.out.println(dateTime1.until(dateTime2, ChronoUnit.MINUTES)); // -60
System.out.println(dateTime1.until(dateTime2, ChronoUnit.SECONDS)); // -3600
System.out.println(dateTime1.until(dateTime2, ChronoUnit.NANOS)); // -3600000000000
final var dateTime1 = ZonedDateTime.of(2100, 1, 1, 1, 2, 0, 0, ZoneOffset.UTC);
final var dateTime2 = ZonedDateTime.of(2100, 1, 1, 3, 32, 15, 123, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-01-01T01:02Z
System.out.println(dateTime2); // 2100-01-01T03:32:15.000000123Z
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 = ZonedDateTime.of(2100, 8, 14, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime2 = ZonedDateTime.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
ZonedDateTime with (TemporalAdjuster adjuster)
final var dateTime = ZonedDateTime.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 = ZonedDateTime.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
ZonedDateTime with (TemporalField field, long newValue)
final var dateTime = ZonedDateTime.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 = ZonedDateTime.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
ZonedDateTime withDayOfMonth (int dayOfMonth)
final var dateTime = ZonedDateTime.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
ZonedDateTime withDayOfYear (int dayOfYear)
final var dateTime = ZonedDateTime.of(2100, 4, 8, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-08T00:00Z
System.out.println(dateTime.withDayOfYear(1)); // 2100-01-01T00:00Z
System.out.println(dateTime.withDayOfYear(2)); // 2100-01-02T00:00Z
System.out.println(dateTime.withDayOfYear(30)); // 2100-01-30T00:00Z
System.out.println(dateTime.withDayOfYear(31)); // 2100-01-31T00:00Z
System.out.println(dateTime.withDayOfYear(32)); // 2100-02-01T00:00Z
System.out.println(dateTime.withDayOfYear(33)); // 2100-02-02T00:00Z
System.out.println(dateTime.withDayOfYear(365)); // 2100-12-31T00:00Z
ZonedDateTime withEarlierOffsetAtOverlap ()
final var zone = ZoneId.of("Europe/Paris");
final var local = LocalDateTime.of(2100, 10, 31, 2, 30);
System.out.println(local); // 2100-10-31T02:30
{
final var dateTime = ZonedDateTime.ofLocal(local, zone, ZoneOffset.ofHours(1));
System.out.println(dateTime); // 2100-10-31T02:30+01:00[Europe/Paris]
final var ret = dateTime.withEarlierOffsetAtOverlap();
System.out.println(ret); // 2100-10-31T02:30+02:00[Europe/Paris]
}
{
final var dateTime = ZonedDateTime.ofLocal(local, zone, ZoneOffset.ofHours(2));
System.out.println(dateTime); // 2100-10-31T02:30+02:00[Europe/Paris]
final var ret = dateTime.withEarlierOffsetAtOverlap();
System.out.println(ret); // 2100-10-31T02:30+02:00[Europe/Paris]
}
ZonedDateTime withFixedOffsetZone ()
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var dateTime = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, zone);
System.out.println(dateTime); // 2100-01-01T00:00-01:00[Etc/GMT+1]
System.out.println(dateTime.withFixedOffsetZone()); // 2100-01-01T00:00-01:00
final var dateTime = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
System.out.println(dateTime.withFixedOffsetZone()); // 2100-01-01T00:00Z
ZonedDateTime withHour (int hour)
final var dateTime = ZonedDateTime.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
ZonedDateTime withLaterOffsetAtOverlap ()
final var zone = ZoneId.of("Europe/Paris");
final var local = LocalDateTime.of(2100, 10, 31, 2, 30);
System.out.println(local); // 2100-10-31T02:30
{
final var dateTime = ZonedDateTime.ofLocal(local, zone, ZoneOffset.ofHours(1));
System.out.println(dateTime); // 2100-10-31T02:30+01:00[Europe/Paris]
final var ret = dateTime.withLaterOffsetAtOverlap();
System.out.println(ret); // 2100-10-31T02:30+01:00[Europe/Paris]
}
{
final var dateTime = ZonedDateTime.ofLocal(local, zone, ZoneOffset.ofHours(2));
System.out.println(dateTime); // 2100-10-31T02:30+02:00[Europe/Paris]
final var ret = dateTime.withLaterOffsetAtOverlap();
System.out.println(ret); // 2100-10-31T02:30+01:00[Europe/Paris]
}
ZonedDateTime withMinute (int minute)
final var dateTime = ZonedDateTime.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
ZonedDateTime withMonth (int month)
final var dateTime = ZonedDateTime.of(2100, 4, 8, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-08T00:00Z
System.out.println(dateTime.withMonth(1)); // 2100-01-08T00:00Z
System.out.println(dateTime.withMonth(2)); // 2100-02-08T00:00Z
System.out.println(dateTime.withMonth(11)); // 2100-11-08T00:00Z
System.out.println(dateTime.withMonth(12)); // 2100-12-08T00:00Z
ZonedDateTime withNano (int nanoOfSecond)
final var dateTime = ZonedDateTime.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
ZonedDateTime withSecond (int second)
final var dateTime = ZonedDateTime.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
ZonedDateTime withYear (int year)
final var dateTime = ZonedDateTime.of(2100, 4, 8, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-08T00:00Z
System.out.println(dateTime.withYear(1800)); // 1800-04-08T00:00Z
System.out.println(dateTime.withYear(-999)); // -0999-04-08T00:00Z
System.out.println(dateTime.withYear(2030)); // 2030-04-08T00:00Z
ZonedDateTime withZoneSameInstant (ZoneId zone)
final var dateTime = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var ret = dateTime.withZoneSameInstant(zone);
System.out.println(ret); // 2099-12-31T23:00-01:00[Etc/GMT+1]
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var dateTime = ZonedDateTime.of(2099, 12, 31, 23, 0, 0, 0, zone);
System.out.println(dateTime); // 2099-12-31T23:00-01:00[Etc/GMT+1]
final var ret = dateTime.withZoneSameInstant(ZoneOffset.UTC);
System.out.println(ret); // 2100-01-01T00:00Z
final var offset = ZoneOffset.ofHours(-4);
System.out.println(offset); // -04:00
final var dateTime = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, offset);
System.out.println(dateTime); // 2100-01-01T00:00-04:00
final var ret = dateTime.withZoneSameInstant(ZoneOffset.UTC);
System.out.println(ret); // 2100-01-01T04:00Z
ZonedDateTime withZoneSameLocal (ZoneId zone)
final var dateTime = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var ret = dateTime.withZoneSameLocal(zone);
System.out.println(ret); // 2100-01-01T00:00-01:00[Etc/GMT+1]
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var dateTime = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, zone);
System.out.println(dateTime); // 2100-01-01T00:00-01:00[Etc/GMT+1]
final var ret = dateTime.withZoneSameLocal(ZoneOffset.UTC);
System.out.println(ret); // 2100-01-01T00:00Z
final var offset = ZoneOffset.ofHours(-4);
System.out.println(offset); // -04:00
final var dateTime = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, offset);
System.out.println(dateTime); // 2100-01-01T00:00-04:00
final var ret = dateTime.withZoneSameLocal(ZoneOffset.UTC);
System.out.println(ret); // 2100-01-01T00:00Z
Methods declared in ChronoZonedDateTime
compareTo, getChronology, isAfter, isBefore, isEqual, toEpochSecond, toInstant
Consulte el siguiente enlace.
Related posts
- Ejemplos de API
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZoneId
- ZoneOffset