Java : ChronoLocalDateTime con ejemplos
ChronoLocalDateTime (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de ChronoLocalDateTime<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 ChronoLocalDateTime<LocalDate> dateTime = LocalDateTime.of(2100, 4, 10, 13, 30);
System.out.println(dateTime); // 2100-04-10T13:30
System.out.println(dateTime.plus(Period.ofDays(5))); // 2100-04-15T13:30
System.out.println(dateTime.minus(Period.ofYears(101))); // 1999-04-10T13:30
System.out.println(dateTime.plus(Duration.ofHours(3))); // 2100-04-10T16:30
System.out.println(dateTime.minus(Duration.ofMinutes(25))); // 2100-04-10T13:05
Methods
default Temporal adjustInto (Temporal temporal)
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var temporal = ZonedDateTime.of(2100, 4, 7, 13, 30, 0, 0, zone);
System.out.println(temporal); // 2100-04-07T13:30-01:00[Etc/GMT+1]
final ChronoLocalDateTime<LocalDate> dateTime = LocalDateTime.of(1800, 1, 2, 3, 4);
System.out.println(dateTime); // 1800-01-02T03:04
final var ret = dateTime.adjustInto(temporal);
System.out.println(ret); // 1800-01-02T03:04-01:00[Etc/GMT+1]
ChronoZonedDateTime<D> atZone (ZoneId zone)
final ChronoLocalDateTime<LocalDate> dateTime = LocalDateTime.of(2100, 4, 10, 13, 30);
System.out.println(dateTime); // 2100-04-10T13:30
final var ret = dateTime.atZone(ZoneOffset.UTC);
System.out.println(ret); // 2100-04-10T13:30Z
default int compareTo (ChronoLocalDateTime<?> other)
final ChronoLocalDateTime<LocalDate> dateTime1 = LocalDateTime.of(2100, 1, 1, 0, 0);
final ChronoLocalDateTime<LocalDate> 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 ChronoLocalDateTime<LocalDate> dateTime1 = LocalDateTime.of(2100, 4, 14, 15, 20);
final ChronoLocalDateTime<LocalDate> 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 ChronoLocalDateTime<LocalDate> dateTime1 = LocalDateTime.of(2100, 4, 15, 0, 0);
final ChronoLocalDateTime<LocalDate> 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
boolean equals (Object obj)
final ChronoLocalDateTime<LocalDate> dateTime1 = LocalDateTime.of(2100, 1, 1, 0, 0);
final ChronoLocalDateTime<LocalDate> 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 ChronoLocalDateTime<LocalDate> dateTime1 = LocalDateTime.of(2100, 4, 14, 15, 20);
final ChronoLocalDateTime<LocalDate> 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 ChronoLocalDateTime<LocalDate> dateTime1 = LocalDateTime.of(2100, 4, 15, 0, 0);
final ChronoLocalDateTime<LocalDate> 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
default String format (DateTimeFormatter formatter)
final ChronoLocalDateTime<LocalDate> dateTime = LocalDateTime.of(2100, 4, 10, 13, 30);
System.out.println(dateTime); // 2100-04-10T13:30
final var ret = dateTime.format(DateTimeFormatter.ISO_DATE_TIME);
System.out.println(ret); // 2100-04-10T13:30:00
static ChronoLocalDateTime<?> from (TemporalAccessor temporal)
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var temporal = ZonedDateTime.of(2100, 4, 15, 13, 30, 0, 0, zone);
System.out.println(temporal); // 2100-04-15T13:30-01:00[Etc/GMT+1]
System.out.println(ChronoLocalDateTime.from(temporal)); // 2100-04-15T13:30
default Chronology getChronology ()
final ChronoLocalDateTime<LocalDate> dateTime = LocalDateTime.of(2100, 4, 10, 13, 30);
System.out.println(dateTime); // 2100-04-10T13:30
System.out.println(dateTime.getChronology()); // ISO
int hashCode ()
final ChronoLocalDateTime<LocalDate> dateTime1 = LocalDateTime.of(2100, 4, 10, 13, 30);
System.out.println(dateTime1.hashCode()); // -1845627591
final ChronoLocalDateTime<LocalDate> dateTime2 = LocalDateTime.of(1800, 1, 1, 0, 0);
System.out.println(dateTime2.hashCode()); // 3686465
default boolean isAfter (ChronoLocalDateTime<?> other)
final ChronoLocalDateTime<LocalDate> dateTime = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00
final var dateTime2 = LocalDateTime.of(2099, 12, 31, 23, 59);
final var dateTime3 = LocalDateTime.of(2100, 1, 1, 0, 0);
final var dateTime4 = LocalDateTime.of(2100, 1, 2, 0, 0);
System.out.println(dateTime2); // 2099-12-31T23:59
System.out.println(dateTime3); // 2100-01-01T00:00
System.out.println(dateTime4); // 2100-01-02T00:00
System.out.println(dateTime.isAfter(dateTime2)); // true
System.out.println(dateTime.isAfter(dateTime3)); // false
System.out.println(dateTime.isAfter(dateTime4)); // false
default boolean isBefore (ChronoLocalDateTime<?> other)
final ChronoLocalDateTime<LocalDate> dateTime = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00
final var dateTime2 = LocalDateTime.of(2099, 12, 31, 23, 59);
final var dateTime3 = LocalDateTime.of(2100, 1, 1, 0, 0);
final var dateTime4 = LocalDateTime.of(2100, 1, 2, 0, 0);
System.out.println(dateTime2); // 2099-12-31T23:59
System.out.println(dateTime3); // 2100-01-01T00:00
System.out.println(dateTime4); // 2100-01-02T00:00
System.out.println(dateTime.isBefore(dateTime2)); // false
System.out.println(dateTime.isBefore(dateTime3)); // false
System.out.println(dateTime.isBefore(dateTime4)); // true
default boolean isEqual (ChronoLocalDateTime<?> other)
final ChronoLocalDateTime<LocalDate> dateTime = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00
final var dateTime2 = LocalDateTime.of(2099, 12, 31, 23, 59);
final var dateTime3 = LocalDateTime.of(2100, 1, 1, 0, 0);
final var dateTime4 = LocalDateTime.of(2100, 1, 2, 0, 0);
System.out.println(dateTime2); // 2099-12-31T23:59
System.out.println(dateTime3); // 2100-01-01T00:00
System.out.println(dateTime4); // 2100-01-02T00:00
System.out.println(dateTime.isEqual(dateTime2)); // false
System.out.println(dateTime.isEqual(dateTime3)); // true
System.out.println(dateTime.isEqual(dateTime4)); // false
boolean isSupported (TemporalField field)
final ChronoLocalDateTime<LocalDate> dateTime = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00
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
default boolean isSupported (TemporalUnit unit)
final ChronoLocalDateTime<LocalDate> dateTime = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00
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 ChronoLocalDateTime<D> minus (long amountToSubtract, TemporalUnit unit)
final ChronoLocalDateTime<LocalDate> 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 ChronoLocalDateTime<LocalDate> 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
default ChronoLocalDateTime<D> minus (TemporalAmount amount)
final ChronoLocalDateTime<LocalDate> 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 ChronoLocalDateTime<LocalDate> 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
ChronoLocalDateTime<D> plus (long amountToAdd, TemporalUnit unit)
final ChronoLocalDateTime<LocalDate> 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 ChronoLocalDateTime<LocalDate> 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
default ChronoLocalDateTime<D> plus (TemporalAmount amount)
final ChronoLocalDateTime<LocalDate> 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 ChronoLocalDateTime<LocalDate> 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
default <R> R query (TemporalQuery<R> query)
final ChronoLocalDateTime<LocalDate> dateTime = LocalDateTime.of(2100, 4, 10, 13, 30);
System.out.println(dateTime); // 2100-04-10T13:30
System.out.println(dateTime.query(TemporalQueries.precision())); // Nanos
static Comparator<ChronoLocalDateTime<?>> timeLineOrder ()
final ChronoLocalDateTime<LocalDate> dateTime1 = LocalDateTime.of(2100, 1, 1, 0, 0);
final ChronoLocalDateTime<LocalDate> dateTime2 = LocalDateTime.of(2099, 12, 31, 0, 0);
System.out.println(dateTime1); // 2100-01-01T00:00
System.out.println(dateTime2); // 2099-12-31T00:00
System.out.println(dateTime1.compareTo(dateTime2)); // 1
final var comparator = ChronoLocalDateTime.timeLineOrder();
System.out.println(comparator.compare(dateTime1, dateTime2)); // 1
default long toEpochSecond (ZoneOffset offset)
final ChronoLocalDateTime<LocalDate> dateTime = LocalDateTime.of(2100, 4, 8, 12, 30);
System.out.println(dateTime); // 2100-04-08T12:30
final long epochSecond1 = dateTime.toEpochSecond(ZoneOffset.UTC);
System.out.println(epochSecond1); // 4110870600
System.out.println(Instant.ofEpochSecond(epochSecond1)); // 2100-04-08T12:30:00Z
final long epochSecond2 = dateTime.toEpochSecond(ZoneOffset.ofHours(9));
System.out.println(epochSecond2); // 4110838200
System.out.println(Instant.ofEpochSecond(epochSecond2)); // 2100-04-08T03:30:00Z
default Instant toInstant (ZoneOffset offset)
final ChronoLocalDateTime<LocalDate> dateTime = LocalDateTime.of(2100, 4, 8, 12, 30);
System.out.println(dateTime); // 2100-04-08T12:30
final var instant1 = dateTime.toInstant(ZoneOffset.UTC);
System.out.println(instant1); // 2100-04-08T12:30:00Z
final var instant2 = dateTime.toInstant(ZoneOffset.ofHours(9));
System.out.println(instant2); // 2100-04-08T03:30:00Z
D toLocalDate ()
final ChronoLocalDateTime<LocalDate> dateTime = LocalDateTime.of(2100, 4, 14, 12, 30);
System.out.println(dateTime); // 2100-04-14T12:30
System.out.println(dateTime.toLocalDate()); // 2100-04-14
final ChronoLocalDateTime<LocalDate> dateTime = LocalDateTime.of(1800, 1, 1, 0, 0);
System.out.println(dateTime); // 1800-01-01T00:00
System.out.println(dateTime.toLocalDate()); // 1800-01-01
LocalTime toLocalTime ()
final ChronoLocalDateTime<LocalDate> dateTime = LocalDateTime.of(2100, 1, 1, 2, 3, 4);
System.out.println(dateTime); // 2100-01-01T02:03:04
System.out.println(dateTime.toLocalTime()); // 02:03:04
final ChronoLocalDateTime<LocalDate> dateTime = LocalDateTime.of(1800, 1, 1, 0, 0);
System.out.println(dateTime); // 1800-01-01T00:00
System.out.println(dateTime.toLocalTime()); // 00:00
String toString ()
final ChronoLocalDateTime<LocalDate> dateTime =
LocalDateTime.of(2100, 4, 14, 13, 30, 45);
final var str = dateTime.toString();
System.out.println(str); // 2100-04-14T13:30:45
final ChronoLocalDateTime<LocalDate> dateTime =
LocalDateTime.of(1800, 1, 1, 0, 0, 0, 123456789);
final var str = dateTime.toString();
System.out.println(str); // 1800-01-01T00:00:00.123456789
default ChronoLocalDateTime<D> with (TemporalAdjuster adjuster)
final ChronoLocalDateTime<LocalDate> 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 ChronoLocalDateTime<LocalDate> 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
ChronoLocalDateTime<D> with (TemporalField field, long newValue)
final ChronoLocalDateTime<LocalDate> 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
Methods declared in Temporal
Methods declared in TemporalAccessor
Related posts
- Ejemplos de API
- Calendar
- ChronoLocalDate
- 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
- ZonedDateTime
- ZoneId
- ZoneOffset