Java : ChronoLocalDate with Examples

ChronoLocalDate (Java SE 21 & JDK 21) with Examples.
You will find code examples on most ChronoLocalDate methods.

Warning :


Summary

A date without time-of-day or time-zone in an arbitrary chronology, intended for advanced globalization use cases.

Class diagram

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)

Adjusts the specified temporal object to have the same date as this object.

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)

Combines this date with a time to create a ChronoLocalDateTime.

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)

Compares this date to another date, including the chronology.

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)

Checks if this date is equal to another date, including the chronology.

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)

Formats this date using the specified 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)

Obtains an instance of ChronoLocalDate from a temporal object.

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 ()

Gets the chronology of this date.

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 ()

Gets the era, as defined by the chronology.

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 ()

A hash code for this date.

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)

Checks if this date is after the specified date ignoring the chronology.

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)

Checks if this date is before the specified date ignoring the chronology.

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)

Checks if this date is equal to the specified date ignoring the chronology.

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 ()

Checks if the year is a leap year, as defined by the calendar system.

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)

Checks if the specified field is supported.

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)

Checks if the specified unit is supported.

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 ()

Returns the length of the month represented by this date, as defined by the calendar system.

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 ()

Returns the length of the year represented by this date, as defined by the calendar system.

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)

Returns an object of the same type as this object with the specified period subtracted.

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)

Returns an object of the same type as this object with an amount subtracted.

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)

Returns an object of the same type as this object with the specified period added.

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)

Returns an object of the same type as this object with an amount added.

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)

Queries this date using the specified 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 ()

Gets a comparator that compares ChronoLocalDate in time-line order ignoring the chronology.

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 ()

Converts this date to the Epoch Day.

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 ()

Outputs this date as a String.

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)

Calculates the period between this date and another date as a ChronoPeriod.

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)

Calculates the amount of time until another date in terms of the specified 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)

Returns an adjusted object of the same type as this object with the adjustment made.

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)

Returns an object of the same type as this object with the specified field altered.

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

get, getLong, range

Please see the link below.


Related posts

To top of page