Java : ChronoLocalDate con ejemplos
ChronoLocalDate (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de 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 ChronoLocalDate date = LocalDate.of(2100, 4, 10);
System.out.println(date); // 2100-04-10
System.out.println(date.plus(Period.ofDays(5))); // 2100-04-15
System.out.println(date.minus(Period.ofYears(101))); // 1999-04-10
Methods
default Temporal adjustInto (Temporal temporal)
final var temporal = LocalDateTime.of(2100, 4, 7, 12, 30);
System.out.println(temporal); // 2100-04-07T12:30
final ChronoLocalDate date = LocalDate.of(1999, 1, 2);
System.out.println(date); // 1999-01-02
final var ret = date.adjustInto(temporal);
System.out.println(ret); // 1999-01-02T12:30
default ChronoLocalDateTime<?> atTime (LocalTime localTime)
final ChronoLocalDate date = LocalDate.of(2100, 4, 15);
System.out.println(date); // 2100-04-15
{
final var time = LocalTime.of(12, 30);
System.out.println(time); // 12:30
System.out.println(date.atTime(time)); // 2100-04-15T12:30
}
{
final var time = LocalTime.of(12, 30, 45);
System.out.println(time); // 12:30:45
System.out.println(date.atTime(time)); // 2100-04-15T12:30:45
}
{
final var time = LocalTime.of(12, 30, 45, 123456789);
System.out.println(time); // 12:30:45.123456789
System.out.println(date.atTime(time)); // 2100-04-15T12:30:45.123456789
}
default int compareTo (ChronoLocalDate other)
final ChronoLocalDate date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01
final ChronoLocalDate date2 = LocalDate.of(2100, 1, 1);
System.out.println(date2); // 2100-01-01
System.out.println(date1.compareTo(date2)); // 0
System.out.println(date2.compareTo(date1)); // 0
final ChronoLocalDate date1 = LocalDate.of(2100, 4, 15);
System.out.println(date1); // 2100-04-15
final ChronoLocalDate date2 = LocalDate.of(2100, 3, 31);
System.out.println(date2); // 2100-03-31
System.out.println(date1.compareTo(date2)); // 1
System.out.println(date2.compareTo(date1)); // -1
final ChronoLocalDate date1 = LocalDate.of(1999, 12, 31);
System.out.println(date1); // 1999-12-31
final ChronoLocalDate date2 = LocalDate.of(2000, 1, 1);
System.out.println(date2); // 2000-01-01
System.out.println(date1.compareTo(date2)); // -1
System.out.println(date2.compareTo(date1)); // 1
boolean equals (Object obj)
final ChronoLocalDate date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01
final ChronoLocalDate date2 = LocalDate.of(2100, 1, 1);
System.out.println(date2); // 2100-01-01
System.out.println(date1.equals(date2)); // true
System.out.println(date2.equals(date1)); // true
final ChronoLocalDate date1 = LocalDate.of(2100, 4, 15);
System.out.println(date1); // 2100-04-15
final ChronoLocalDate date2 = LocalDate.of(2100, 3, 31);
System.out.println(date2); // 2100-03-31
System.out.println(date1.equals(date2)); // false
System.out.println(date2.equals(date1)); // false
final ChronoLocalDate date1 = LocalDate.of(1999, 12, 31);
System.out.println(date1); // 1999-12-31
final ChronoLocalDate date2 = LocalDate.of(2000, 1, 1);
System.out.println(date2); // 2000-01-01
System.out.println(date1.equals(date2)); // false
System.out.println(date2.equals(date1)); // false
default String format (DateTimeFormatter formatter)
final ChronoLocalDate date = LocalDate.of(2100, 4, 15);
System.out.println(date); // 2100-04-15
final var ret1 = date.format(DateTimeFormatter.ISO_DATE);
System.out.println(ret1); // 2100-04-15
final var ret2 = date.format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(ret2); // 21000415
static ChronoLocalDate from (TemporalAccessor temporal)
final var temporal = LocalDateTime.of(2100, 4, 15, 12, 30, 55);
System.out.println(temporal); // 2100-04-15T12:30:55
System.out.println(ChronoLocalDate.from(temporal)); // 2100-04-15
final var temporal = OffsetDateTime.of(1999, 12, 1, 22, 30, 55, 0,
ZoneOffset.ofHours(12));
System.out.println(temporal); // 1999-12-01T22:30:55+12:00
System.out.println(ChronoLocalDate.from(temporal)); // 1999-12-01
Chronology getChronology ()
final ChronoLocalDate date = LocalDate.of(2100, 4, 15);
System.out.println(date); // 2100-04-15
System.out.println(date.getChronology()); // ISO
System.out.println(date.getEra()); // CE
final ChronoLocalDate date = JapaneseDate.of(1999, 1, 2);
System.out.println(date); // Japanese Heisei 11-01-02
System.out.println(date.getChronology()); // Japanese
System.out.println(date.getEra()); // Heisei
default Era getEra ()
final ChronoLocalDate date = LocalDate.of(2100, 4, 15);
System.out.println(date); // 2100-04-15
System.out.println(date.getChronology()); // ISO
System.out.println(date.getEra()); // CE
final ChronoLocalDate date = JapaneseDate.of(1999, 1, 2);
System.out.println(date); // Japanese Heisei 11-01-02
System.out.println(date.getChronology()); // Japanese
System.out.println(date.getEra()); // Heisei
int hashCode ()
final ChronoLocalDate date1 = LocalDate.of(1970, 1, 1);
System.out.println(date1.hashCode()); // 4034625
final ChronoLocalDate date2 = LocalDate.of(1970, 1, 2);
System.out.println(date2.hashCode()); // 4034626
final ChronoLocalDate date3 = LocalDate.of(2100, 4, 15);
System.out.println(date3.hashCode()); // 4303119
final ChronoLocalDate date4 = LocalDate.of(9999, 1, 1);
System.out.println(date4.hashCode()); // 20469825
default boolean isAfter (ChronoLocalDate other)
final ChronoLocalDate date = LocalDate.of(2100, 1, 1);
System.out.println(date); // 2100-01-01
final ChronoLocalDate date2 = LocalDate.of(2099, 12, 31);
final ChronoLocalDate date3 = LocalDate.of(2100, 1, 1);
final ChronoLocalDate date4 = LocalDate.of(2100, 1, 2);
System.out.println(date2); // 2099-12-31
System.out.println(date3); // 2100-01-01
System.out.println(date4); // 2100-01-02
System.out.println(date.isAfter(date2)); // true
System.out.println(date.isAfter(date3)); // false
System.out.println(date.isAfter(date4)); // false
default boolean isBefore (ChronoLocalDate other)
final ChronoLocalDate date = LocalDate.of(2100, 1, 1);
System.out.println(date); // 2100-01-01
final ChronoLocalDate date2 = LocalDate.of(2099, 12, 31);
final ChronoLocalDate date3 = LocalDate.of(2100, 1, 1);
final ChronoLocalDate date4 = LocalDate.of(2100, 1, 2);
System.out.println(date2); // 2099-12-31
System.out.println(date3); // 2100-01-01
System.out.println(date4); // 2100-01-02
System.out.println(date.isBefore(date2)); // false
System.out.println(date.isBefore(date3)); // false
System.out.println(date.isBefore(date4)); // true
default boolean isEqual (ChronoLocalDate other)
final ChronoLocalDate date = LocalDate.of(2100, 1, 1);
System.out.println(date); // 2100-01-01
final ChronoLocalDate date2 = LocalDate.of(2099, 12, 31);
final ChronoLocalDate date3 = LocalDate.of(2100, 1, 1);
final ChronoLocalDate date4 = LocalDate.of(2100, 1, 2);
System.out.println(date2); // 2099-12-31
System.out.println(date3); // 2100-01-01
System.out.println(date4); // 2100-01-02
System.out.println(date.isEqual(date2)); // false
System.out.println(date.isEqual(date3)); // true
System.out.println(date.isEqual(date4)); // false
default boolean isLeapYear ()
final ChronoLocalDate date = LocalDate.of(2103, Month.FEBRUARY, 1);
System.out.println(date); // 2103-02-01
System.out.println(date.isLeapYear()); // false
System.out.println(date.lengthOfYear()); // 365
final ChronoLocalDate date = LocalDate.of(2104, Month.FEBRUARY, 1);
System.out.println(date); // 2104-02-01
System.out.println(date.isLeapYear()); // true
System.out.println(date.lengthOfYear()); // 366
default boolean isSupported (TemporalField field)
final ChronoLocalDate date = LocalDate.of(2100, Month.JANUARY, 1);
System.out.println(date); // 2100-01-01
System.out.println("-- supported fields --");
for (final var field : ChronoField.values()) {
if (date.isSupported(field)) {
System.out.println(field);
}
}
// Result
// ↓
//-- supported fields --
//DayOfWeek
//AlignedDayOfWeekInMonth
//AlignedDayOfWeekInYear
//DayOfMonth
//DayOfYear
//EpochDay
//AlignedWeekOfMonth
//AlignedWeekOfYear
//MonthOfYear
//ProlepticMonth
//YearOfEra
//Year
//Era
default boolean isSupported (TemporalUnit unit)
final ChronoLocalDate date = LocalDate.of(2100, Month.JANUARY, 1);
System.out.println(date); // 2100-01-01
System.out.println("-- supported units --");
for (final var unit : ChronoUnit.values()) {
if (date.isSupported(unit)) {
System.out.println(unit);
}
}
// Result
// ↓
//-- supported units --
//Days
//Weeks
//Months
//Years
//Decades
//Centuries
//Millennia
//Eras
int lengthOfMonth ()
final ChronoLocalDate date = LocalDate.of(2103, Month.JANUARY, 1);
System.out.println(date); // 2103-01-01
System.out.println(date.lengthOfMonth()); // 31
final ChronoLocalDate date = LocalDate.of(2103, Month.FEBRUARY, 1);
System.out.println(date); // 2103-02-01
System.out.println(date.isLeapYear()); // false
System.out.println(date.lengthOfMonth()); // 28
final ChronoLocalDate date = LocalDate.of(2104, Month.FEBRUARY, 1);
System.out.println(date); // 2104-02-01
System.out.println(date.isLeapYear()); // true
System.out.println(date.lengthOfMonth()); // 29
default int lengthOfYear ()
final ChronoLocalDate date = LocalDate.of(2103, Month.FEBRUARY, 1);
System.out.println(date); // 2103-02-01
System.out.println(date.isLeapYear()); // false
System.out.println(date.lengthOfYear()); // 365
final ChronoLocalDate date = LocalDate.of(2104, Month.FEBRUARY, 1);
System.out.println(date); // 2104-02-01
System.out.println(date.isLeapYear()); // true
System.out.println(date.lengthOfYear()); // 366
default ChronoLocalDate minus (long amountToSubtract, TemporalUnit unit)
final ChronoLocalDate date = LocalDate.of(2100, Month.MAY, 20);
System.out.println(date); // 2100-05-20
System.out.println(date.minus(1, ChronoUnit.DAYS)); // 2100-05-19
System.out.println(date.minus(2, ChronoUnit.MONTHS)); // 2100-03-20
System.out.println(date.minus(3, ChronoUnit.YEARS)); // 2097-05-20
default ChronoLocalDate minus (TemporalAmount amount)
final ChronoLocalDate date = LocalDate.of(2100, Month.MAY, 20);
System.out.println(date); // 2100-05-20
System.out.println(date.minus(Period.ofDays(1))); // 2100-05-19
System.out.println(date.minus(Period.ofMonths(2))); // 2100-03-20
System.out.println(date.minus(Period.ofYears(3))); // 2097-05-20
System.out.println(date.minus(Period.of(1, 2, 3))); // 2099-03-17
default ChronoLocalDate plus (long amountToAdd, TemporalUnit unit)
final ChronoLocalDate date = LocalDate.of(2100, Month.MAY, 20);
System.out.println(date); // 2100-05-20
System.out.println(date.plus(1, ChronoUnit.DAYS)); // 2100-05-21
System.out.println(date.plus(2, ChronoUnit.MONTHS)); // 2100-07-20
System.out.println(date.plus(3, ChronoUnit.YEARS)); // 2103-05-20
default ChronoLocalDate plus (TemporalAmount amount)
final ChronoLocalDate date = LocalDate.of(2100, Month.MAY, 20);
System.out.println(date); // 2100-05-20
System.out.println(date.plus(Period.ofDays(1))); // 2100-05-21
System.out.println(date.plus(Period.ofMonths(2))); // 2100-07-20
System.out.println(date.plus(Period.ofYears(3))); // 2103-05-20
System.out.println(date.plus(Period.of(1, 2, 3))); // 2101-07-23
default <R> R query (TemporalQuery<R> query)
final ChronoLocalDate date = LocalDate.of(2100, Month.APRIL, 15);
System.out.println(date); // 2100-04-15
System.out.println(date.query(TemporalQueries.precision())); // Days
static Comparator<ChronoLocalDate> timeLineOrder ()
final var date1 = LocalDate.of(2019, 10, 15);
final var date2 = JapaneseDate.of(2019, 10, 15);
System.out.println(date1); // 2019-10-15
System.out.println(date2); // Japanese Reiwa 1-10-15
System.out.println(date1.compareTo(date2)); // -1
final var comparator = ChronoLocalDate.timeLineOrder();
System.out.println(comparator.compare(date1, date2)); // 0
default long toEpochDay ()
final ChronoLocalDate date = LocalDate.of(2100, 4, 8);
System.out.println(date); // 2100-04-08
System.out.println(date.toEpochDay()); // 47579
final ChronoLocalDate date = LocalDate.of(1970, 1, 1);
System.out.println(date); // 1970-01-01
System.out.println(date.toEpochDay()); // 0
final ChronoLocalDate date = LocalDate.of(1970, 1, 2);
System.out.println(date); // 1970-01-02
System.out.println(date.toEpochDay()); // 1
final ChronoLocalDate date = LocalDate.of(1970, 2, 1);
System.out.println(date); // 1970-02-01
System.out.println(date.toEpochDay()); // 31
String toString ()
final ChronoLocalDate date = LocalDate.of(2100, Month.JANUARY, 15);
final var str = date.toString();
System.out.println(str); // 2100-01-15
final ChronoLocalDate date = LocalDate.of(1999, Month.OCTOBER, 31);
final var str = date.toString();
System.out.println(str); // 1999-10-31
ChronoPeriod until (ChronoLocalDate endDateExclusive)
final ChronoLocalDate date1 = LocalDate.of(2100, Month.JANUARY, 15);
final ChronoLocalDate date2 = LocalDate.of(2101, Month.MARCH, 18);
System.out.println(date1); // 2100-01-15
System.out.println(date2); // 2101-03-18
final var period1 = date1.until(date2);
System.out.println(period1.get(ChronoUnit.YEARS)); // 1
System.out.println(period1.get(ChronoUnit.MONTHS)); // 2
System.out.println(period1.get(ChronoUnit.DAYS)); // 3
final var period2 = date2.until(date1);
System.out.println(period2.get(ChronoUnit.YEARS)); // -1
System.out.println(period2.get(ChronoUnit.MONTHS)); // -2
System.out.println(period2.get(ChronoUnit.DAYS)); // -3
final ChronoLocalDate date1 = LocalDate.of(2100, Month.FEBRUARY, 2);
final ChronoLocalDate date2 = LocalDate.of(2100, Month.MARCH, 1);
System.out.println(date1); // 2100-02-02
System.out.println(date2); // 2100-03-01
final var period = date1.until(date2);
System.out.println(period.get(ChronoUnit.YEARS)); // 0
System.out.println(period.get(ChronoUnit.MONTHS)); // 0
System.out.println(period.get(ChronoUnit.DAYS)); // 27
final ChronoLocalDate date1 = LocalDate.of(2100, Month.FEBRUARY, 1);
final ChronoLocalDate date2 = LocalDate.of(2100, Month.MAY, 1);
System.out.println(date1); // 2100-02-01
System.out.println(date2); // 2100-05-01
final var period = date1.until(date2);
System.out.println(period.get(ChronoUnit.YEARS)); // 0
System.out.println(period.get(ChronoUnit.MONTHS)); // 3
System.out.println(period.get(ChronoUnit.DAYS)); // 0
long until (Temporal endExclusive, TemporalUnit unit)
final ChronoLocalDate date1 = LocalDate.of(2100, Month.JANUARY, 15);
final ChronoLocalDate date2 = LocalDate.of(2101, Month.MARCH, 18);
System.out.println(date1); // 2100-01-15
System.out.println(date2); // 2101-03-18
System.out.println(date1.until(date2, ChronoUnit.YEARS)); // 1
System.out.println(date1.until(date2, ChronoUnit.MONTHS)); // 14
System.out.println(date1.until(date2, ChronoUnit.DAYS)); // 427
final ChronoLocalDate date1 = LocalDate.of(2100, Month.OCTOBER, 31);
final ChronoLocalDate date2 = LocalDate.of(2100, Month.DECEMBER, 10);
System.out.println(date1); // 2100-10-31
System.out.println(date2); // 2100-12-10
System.out.println(date1.until(date2, ChronoUnit.YEARS)); // 0
System.out.println(date1.until(date2, ChronoUnit.MONTHS)); // 1
System.out.println(date1.until(date2, ChronoUnit.DAYS)); // 40
default ChronoLocalDate with (TemporalAdjuster adjuster)
final ChronoLocalDate date = LocalDate.of(2100, Month.APRIL, 15);
System.out.println(date); // 2100-04-15
System.out.println(date.with(Year.of(1999))); // 1999-04-15
System.out.println(date.with(Month.JANUARY)); // 2100-01-15
System.out.println(date.with(TemporalAdjusters.firstDayOfMonth())); // 2100-04-01
System.out.println(date.with(TemporalAdjusters.lastDayOfMonth())); // 2100-04-30
default ChronoLocalDate with (TemporalField field, long newValue)
final ChronoLocalDate date = LocalDate.of(2100, Month.APRIL, 15);
System.out.println(date); // 2100-04-15
System.out.println(date.with(ChronoField.YEAR, 1999)); // 1999-04-15
System.out.println(date.with(ChronoField.MONTH_OF_YEAR, 12)); // 2100-12-15
System.out.println(date.with(ChronoField.DAY_OF_MONTH, 30)); // 2100-04-30
Methods declared in TemporalAccessor
Related posts
- Ejemplos de API
- Calendar
- 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
- ZonedDateTime
- ZoneId
- ZoneOffset