Java : ChronoZonedDateTime con ejemplos
ChronoZonedDateTime (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de ChronoZonedDateTime<D extends ChronoLocalDate>.
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 ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(2100, 4, 10, 13, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-10T13:30Z
System.out.println(dateTime.plus(Period.ofDays(5))); // 2100-04-15T13:30Z
System.out.println(dateTime.minus(Period.ofYears(101))); // 1999-04-10T13:30Z
System.out.println(dateTime.plus(Duration.ofHours(3))); // 2100-04-10T16:30Z
System.out.println(dateTime.minus(Duration.ofMinutes(25))); // 2100-04-10T13:05Z
Methods
default int compareTo (ChronoZonedDateTime<?> other)
final ChronoZonedDateTime<LocalDate> dateTime1 =
ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final ChronoZonedDateTime<LocalDate> 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.compareTo(dateTime2)); // 0
final ChronoZonedDateTime<LocalDate> dateTime1 =
ZonedDateTime.of(2100, 4, 14, 15, 20, 0, 0, ZoneOffset.UTC);
final ChronoZonedDateTime<LocalDate> 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.compareTo(dateTime2)); // -1
final ChronoZonedDateTime<LocalDate> dateTime1 =
ZonedDateTime.of(2100, 4, 15, 0, 0, 0, 0, ZoneOffset.UTC);
final ChronoZonedDateTime<LocalDate> 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.compareTo(dateTime2)); // 1
boolean equals (Object obj)
final ChronoZonedDateTime<LocalDate> dateTime1 =
ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final ChronoZonedDateTime<LocalDate> 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 ChronoZonedDateTime<LocalDate> dateTime1 =
ZonedDateTime.of(2100, 4, 14, 15, 20, 0, 0, ZoneOffset.UTC);
final ChronoZonedDateTime<LocalDate> 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 ChronoZonedDateTime<LocalDate> dateTime1 =
ZonedDateTime.of(2100, 4, 15, 0, 0, 0, 0, ZoneOffset.UTC);
final ChronoZonedDateTime<LocalDate> 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
default String format (DateTimeFormatter formatter)
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(2100, 4, 10, 13, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-10T13:30Z
final var ret = dateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
System.out.println(ret); // 2100-04-10T13:30:00Z
static ChronoZonedDateTime<?> from (TemporalAccessor temporal)
final var offset = ZoneOffset.ofHours(1);
System.out.println(offset); // +01:00
final var temporal = OffsetDateTime.of(2100, 4, 15, 13, 30, 0, 0, offset);
System.out.println(temporal); // 2100-04-15T13:30+01:00
System.out.println(ChronoZonedDateTime.from(temporal)); // 2100-04-15T13:30+01:00
default Chronology getChronology ()
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(2100, 4, 10, 13, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-10T13:30Z
System.out.println(dateTime.getChronology()); // ISO
default long getLong (TemporalField field)
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final ChronoZonedDateTime<LocalDate> 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
ZoneOffset getOffset ()
final ChronoZonedDateTime<LocalDate> 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.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final ChronoZonedDateTime<LocalDate> 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.getOffset()); // -01:00
ZoneId getZone ()
final ChronoZonedDateTime<LocalDate> 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.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final ChronoZonedDateTime<LocalDate> 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.getZone()); // Etc/GMT+1
int hashCode ()
final ChronoZonedDateTime<LocalDate> 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 ChronoZonedDateTime<LocalDate> 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
default boolean isAfter (ChronoZonedDateTime<?> other)
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
final var dateTime2 = ZonedDateTime.of(2099, 12, 31, 23, 59, 0, 0, ZoneOffset.UTC);
final var dateTime3 = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime4 = ZonedDateTime.of(2100, 1, 2, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime2); // 2099-12-31T23:59Z
System.out.println(dateTime3); // 2100-01-01T00:00Z
System.out.println(dateTime4); // 2100-01-02T00:00Z
System.out.println(dateTime.isAfter(dateTime2)); // true
System.out.println(dateTime.isAfter(dateTime3)); // false
System.out.println(dateTime.isAfter(dateTime4)); // false
default boolean isBefore (ChronoZonedDateTime<?> other)
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
final var dateTime2 = ZonedDateTime.of(2099, 12, 31, 23, 59, 0, 0, ZoneOffset.UTC);
final var dateTime3 = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime4 = ZonedDateTime.of(2100, 1, 2, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime2); // 2099-12-31T23:59Z
System.out.println(dateTime3); // 2100-01-01T00:00Z
System.out.println(dateTime4); // 2100-01-02T00:00Z
System.out.println(dateTime.isBefore(dateTime2)); // false
System.out.println(dateTime.isBefore(dateTime3)); // false
System.out.println(dateTime.isBefore(dateTime4)); // true
default boolean isEqual (ChronoZonedDateTime<?> other)
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
final var dateTime2 = ZonedDateTime.of(2099, 12, 31, 23, 59, 0, 0, ZoneOffset.UTC);
final var dateTime3 = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final var dateTime4 = ZonedDateTime.of(2100, 1, 2, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime2); // 2099-12-31T23:59Z
System.out.println(dateTime3); // 2100-01-01T00:00Z
System.out.println(dateTime4); // 2100-01-02T00:00Z
System.out.println(dateTime.isEqual(dateTime2)); // false
System.out.println(dateTime.isEqual(dateTime3)); // true
System.out.println(dateTime.isEqual(dateTime4)); // false
boolean isSupported (TemporalField field)
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
System.out.println("-- supported fields --");
for (final var field : ChronoField.values()) {
if (dateTime.isSupported(field)) {
System.out.println(field);
}
}
// Result
// ↓
//-- supported fields --
//NanoOfSecond
//NanoOfDay
//MicroOfSecond
//MicroOfDay
//MilliOfSecond
//MilliOfDay
//SecondOfMinute
//SecondOfDay
//MinuteOfHour
//MinuteOfDay
//HourOfAmPm
//ClockHourOfAmPm
//HourOfDay
//ClockHourOfDay
//AmPmOfDay
//DayOfWeek
//AlignedDayOfWeekInMonth
//AlignedDayOfWeekInYear
//DayOfMonth
//DayOfYear
//EpochDay
//AlignedWeekOfMonth
//AlignedWeekOfYear
//MonthOfYear
//ProlepticMonth
//YearOfEra
//Year
//Era
//InstantSeconds
//OffsetSeconds
default boolean isSupported (TemporalUnit unit)
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T00:00Z
System.out.println("-- supported units --");
for (final var unit : ChronoUnit.values()) {
if (dateTime.isSupported(unit)) {
System.out.println(unit);
}
}
// Result
// ↓
//-- supported units --
//Nanos
//Micros
//Millis
//Seconds
//Minutes
//Hours
//HalfDays
//Days
//Weeks
//Months
//Years
//Decades
//Centuries
//Millennia
//Eras
default ChronoZonedDateTime<D> minus (long amountToSubtract, TemporalUnit unit)
final ChronoZonedDateTime<LocalDate> 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 ChronoZonedDateTime<LocalDate> 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
default ChronoZonedDateTime<D> minus (TemporalAmount amount)
final ChronoZonedDateTime<LocalDate> 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 ChronoZonedDateTime<LocalDate> 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
ChronoZonedDateTime<D> plus (long amountToAdd, TemporalUnit unit)
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(2100, 4, 13, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-13T00:00Z
final var ret1 = dateTime.plus(1, ChronoUnit.YEARS);
System.out.println(ret1); // 2101-04-13T00:00Z
final var ret2 = dateTime.plus(2, ChronoUnit.MONTHS);
System.out.println(ret2); // 2100-06-13T00:00Z
final var ret3 = dateTime.plus(3, ChronoUnit.DAYS);
System.out.println(ret3); // 2100-04-16T00:00Z
final ChronoZonedDateTime<LocalDate> 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
default ChronoZonedDateTime<D> plus (TemporalAmount amount)
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(2100, 4, 13, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-13T00:00Z
final var ret1 = dateTime.plus(Period.ofYears(1));
System.out.println(ret1); // 2101-04-13T00:00Z
final var ret2 = dateTime.plus(Period.ofMonths(2));
System.out.println(ret2); // 2100-06-13T00:00Z
final var ret3 = dateTime.plus(Period.ofDays(3));
System.out.println(ret3); // 2100-04-16T00:00Z
final ChronoZonedDateTime<LocalDate> 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
default <R> R query (TemporalQuery<R> query)
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final ChronoZonedDateTime<LocalDate>
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
static Comparator<ChronoZonedDateTime<?>> timeLineOrder ()
final ChronoZonedDateTime<LocalDate> dateTime1 =
ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
final ChronoZonedDateTime<LocalDate> dateTime2 =
ZonedDateTime.of(2099, 12, 31, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime1); // 2100-01-01T00:00Z
System.out.println(dateTime2); // 2099-12-31T00:00Z
System.out.println(dateTime1.compareTo(dateTime2)); // 1
final var comparator = ChronoZonedDateTime.timeLineOrder();
System.out.println(comparator.compare(dateTime1, dateTime2)); // 1
default long toEpochSecond ()
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(2100, 4, 8, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-08T12:30Z
final long epochSecond = dateTime.toEpochSecond();
System.out.println(epochSecond); // 4110870600
System.out.println(Instant.ofEpochSecond(epochSecond)); // 2100-04-08T12:30:00Z
default Instant toInstant ()
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(2100, 4, 8, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-08T12:30Z
final var instant = dateTime.toInstant();
System.out.println(instant); // 2100-04-08T12:30:00Z
default D toLocalDate ()
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(2100, 4, 14, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T12:30Z
System.out.println(dateTime.toLocalDate()); // 2100-04-14
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 1800-01-01T00:00Z
System.out.println(dateTime.toLocalDate()); // 1800-01-01
ChronoLocalDateTime<D> toLocalDateTime ()
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(2100, 4, 14, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-04-14T12:30Z
System.out.println(dateTime.toLocalDateTime()); // 2100-04-14T12:30
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 1800-01-01T00:00Z
System.out.println(dateTime.toLocalDateTime()); // 1800-01-01T00:00
default LocalTime toLocalTime ()
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(2100, 1, 1, 2, 3, 4, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 2100-01-01T02:03:04Z
System.out.println(dateTime.toLocalTime()); // 02:03:04
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(1800, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 1800-01-01T00:00Z
System.out.println(dateTime.toLocalTime()); // 00:00
String toString ()
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(2100, 4, 14, 13, 30, 45, 0, zone);
final var str = dateTime.toString();
System.out.println(str); // 2100-04-14T13:30:45-01:00[Etc/GMT+1]
final ChronoZonedDateTime<LocalDate> dateTime =
ZonedDateTime.of(1800, 1, 1, 0, 0, 0, 123456789, ZoneOffset.UTC);
final var str = dateTime.toString();
System.out.println(str); // 1800-01-01T00:00:00.123456789Z
default ChronoZonedDateTime<D> with (TemporalAdjuster adjuster)
final ChronoZonedDateTime<LocalDate> 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 ChronoZonedDateTime<LocalDate> 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
ChronoZonedDateTime<D> with (TemporalField field, long newValue)
final ChronoZonedDateTime<LocalDate> 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 ChronoZonedDateTime<LocalDate> 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
ChronoZonedDateTime<D> 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 ChronoZonedDateTime<LocalDate> 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 ChronoZonedDateTime<LocalDate> 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]
}
ChronoZonedDateTime<D> 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 ChronoZonedDateTime<LocalDate> 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 ChronoZonedDateTime<LocalDate> 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]
}
ChronoZonedDateTime<D> withZoneSameInstant (ZoneId zone)
final ChronoZonedDateTime<LocalDate> 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 ChronoZonedDateTime<LocalDate> 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
ChronoZonedDateTime<D> withZoneSameLocal (ZoneId zone)
final ChronoZonedDateTime<LocalDate> 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 ChronoZonedDateTime<LocalDate> 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
Methods declared in Temporal
Methods declared in TemporalAccessor
Related posts
- Ejemplos de API
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- 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
- ZonedDateTime
- ZoneId
- ZoneOffset