Java : LocalDate with Examples

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


Summary

A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.

Class diagram

final var date = LocalDate.of(2100, 12, 15);
System.out.println(date); // 2100-12-15

System.out.println(date.getYear()); // 2100
System.out.println(date.getMonth()); // DECEMBER
System.out.println(date.getDayOfMonth()); // 15

Fields

static final LocalDate EPOCH

The epoch year LocalDate, '1970-01-01'.

System.out.println(LocalDate.EPOCH); // 1970-01-01

static final LocalDate MAX

The maximum supported LocalDate, '+999999999-12-31'.

System.out.println(LocalDate.MAX); // +999999999-12-31
System.out.println(LocalDate.MAX.getYear()); // 999999999
System.out.println(LocalDate.MAX.getMonthValue()); // 12
System.out.println(LocalDate.MAX.getDayOfMonth()); // 31

static final LocalDate MIN

The minimum supported LocalDate, '-999999999-01-01'.

System.out.println(LocalDate.MIN); // -999999999-01-01
System.out.println(LocalDate.MIN.getYear()); // -999999999
System.out.println(LocalDate.MIN.getMonthValue()); // 1
System.out.println(LocalDate.MIN.getDayOfMonth()); // 1

Methods

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 var date = LocalDate.of(1800, 1, 1);
System.out.println(date); // 1800-01-01

final var ret = date.adjustInto(temporal);
System.out.println(ret); // 1800-01-01T12:30
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles

final var temporal = ZonedDateTime.of(2100, 4, 7, 12, 30, 0, 0, zone);
System.out.println(temporal); // 2100-04-07T12:30-07:00[America/Los_Angeles]

final var date = LocalDate.of(2500, 5, 1);
System.out.println(date); // 2500-05-01

final var ret = date.adjustInto(temporal);
System.out.println(ret); // 2500-05-01T12:30-07:00[America/Los_Angeles]

LocalDateTime atStartOfDay ()

Combines this date with the time of midnight to create a LocalDateTime at the start of this date.

final var date = LocalDate.of(2100, 4, 7);
System.out.println(date); // 2100-04-07
System.out.println(date.atStartOfDay()); // 2100-04-07T00:00

final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles

System.out.println(date.atStartOfDay(zone)); // 2100-04-07T00:00-07:00[America/Los_Angeles]
final var date = LocalDate.of(1800, 1, 1);
System.out.println(date); // 1800-01-01
System.out.println(date.atStartOfDay()); // 1800-01-01T00:00

final var zone = ZoneOffset.UTC;
System.out.println(zone); // Z

System.out.println(date.atStartOfDay(zone)); // 1800-01-01T00:00Z

ZonedDateTime atStartOfDay (ZoneId zone)

Returns a zoned date-time from this date at the earliest valid time according to the rules in the time-zone.

final var date = LocalDate.of(2100, 4, 7);
System.out.println(date); // 2100-04-07
System.out.println(date.atStartOfDay()); // 2100-04-07T00:00

final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles

System.out.println(date.atStartOfDay(zone)); // 2100-04-07T00:00-07:00[America/Los_Angeles]
final var date = LocalDate.of(1800, 1, 1);
System.out.println(date); // 1800-01-01
System.out.println(date.atStartOfDay()); // 1800-01-01T00:00

final var zone = ZoneOffset.UTC;
System.out.println(zone); // Z

System.out.println(date.atStartOfDay(zone)); // 1800-01-01T00:00Z

LocalDateTime atTime (int hour, int minute)

Combines this date with a time to create a LocalDateTime.

final var date = LocalDate.of(2100, 4, 7);
System.out.println(date); // 2100-04-07

final var ret1 = date.atTime(12, 30);
System.out.println(ret1); // 2100-04-07T12:30

final var ret2 = date.atTime(12, 30, 45);
System.out.println(ret2); // 2100-04-07T12:30:45

final var ret3 = date.atTime(12, 30, 45, 123456789);
System.out.println(ret3); // 2100-04-07T12:30:45.123456789

LocalDateTime atTime (int hour, int minute, int second)

Combines this date with a time to create a LocalDateTime.

final var date = LocalDate.of(2100, 4, 7);
System.out.println(date); // 2100-04-07

final var ret1 = date.atTime(12, 30);
System.out.println(ret1); // 2100-04-07T12:30

final var ret2 = date.atTime(12, 30, 45);
System.out.println(ret2); // 2100-04-07T12:30:45

final var ret3 = date.atTime(12, 30, 45, 123456789);
System.out.println(ret3); // 2100-04-07T12:30:45.123456789

LocalDateTime atTime (int hour, int minute, int second, int nanoOfSecond)

Combines this date with a time to create a LocalDateTime.

final var date = LocalDate.of(2100, 4, 7);
System.out.println(date); // 2100-04-07

final var ret1 = date.atTime(12, 30);
System.out.println(ret1); // 2100-04-07T12:30

final var ret2 = date.atTime(12, 30, 45);
System.out.println(ret2); // 2100-04-07T12:30:45

final var ret3 = date.atTime(12, 30, 45, 123456789);
System.out.println(ret3); // 2100-04-07T12:30:45.123456789

LocalDateTime atTime (LocalTime time)

Combines this date with a time to create a LocalDateTime.

final var date = LocalDate.of(2100, 4, 7);
System.out.println(date); // 2100-04-07

{
    final var time = LocalTime.of(12, 30);
    System.out.println(time); // 12:30
    System.out.println(date.atTime(time)); // 2100-04-07T12:30
}
{
    final var time = LocalTime.of(12, 30, 45);
    System.out.println(time); // 12:30:45
    System.out.println(date.atTime(time)); // 2100-04-07T12: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-07T12:30:45.123456789
}

OffsetDateTime atTime (OffsetTime time)

Combines this date with an offset time to create an OffsetDateTime.

final var date = LocalDate.of(2100, 4, 7);
System.out.println(date); // 2100-04-07

{
    final var time = LocalTime.of(0, 0);
    final var offset = OffsetTime.of(time, ZoneOffset.UTC);

    System.out.println(offset); // 00:00Z
    System.out.println(date.atTime(offset)); // 2100-04-07T00:00Z
}
{
    final var time = LocalTime.of(0, 0);
    final var offset = OffsetTime.of(time, ZoneOffset.ofHours(9));

    System.out.println(offset); // 00:00+09:00
    System.out.println(date.atTime(offset)); // 2100-04-07T00:00+09:00
}
{
    final var time = LocalTime.of(12, 55);
    final var offset = OffsetTime.of(time, ZoneOffset.ofHoursMinutes(-2, -30));

    System.out.println(offset); // 12:55-02:30
    System.out.println(date.atTime(offset)); // 2100-04-07T12:55-02:30
}

int compareTo (ChronoLocalDate other)

Compares this date to another date.

final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01

final var date2 = LocalDate.of(2100, 1, 1);
System.out.println(date2); // 2100-01-01

System.out.println(date1.compareTo(date2)); // 0
final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01

final var date2 = LocalDate.of(2100, 1, 2);
System.out.println(date2); // 2100-01-02

System.out.println(date1.compareTo(date2)); // -1
final var date1 = LocalDate.of(2100, 2, 1);
System.out.println(date1); // 2100-02-01

final var date2 = LocalDate.of(2100, 1, 31);
System.out.println(date2); // 2100-01-31

System.out.println(date1.compareTo(date2)); // 1
final var date1 = LocalDate.of(2099, 12, 31);
System.out.println(date1); // 2099-12-31

final var date2 = LocalDate.of(2100, 1, 1);
System.out.println(date2); // 2100-01-01

System.out.println(date1.compareTo(date2)); // -1

Stream<LocalDate> datesUntil (LocalDate endExclusive)

Returns a sequential ordered stream of dates.

final var date = LocalDate.of(2100, 1, 30);
System.out.println(date); // 2100-01-30

final var endExclusive = LocalDate.of(2100, 2, 5);
System.out.println(endExclusive); // 2100-02-05

System.out.println("-- datesUntil --");
date.datesUntil(endExclusive).forEach(d -> {
    System.out.println(d);
});

// Result
// ↓
//-- datesUntil --
//2100-01-30
//2100-01-31
//2100-02-01
//2100-02-02
//2100-02-03
//2100-02-04

Stream<LocalDate> datesUntil (LocalDate endExclusive, Period step)

Returns a sequential ordered stream of dates by given incremental step.

final var date = LocalDate.of(2100, 1, 20);
System.out.println(date); // 2100-01-20

final var endExclusive = LocalDate.of(2100, 2, 5);
System.out.println(endExclusive); // 2100-02-05

final var step = Period.ofDays(2);
System.out.println(step); // P2D

System.out.println("-- datesUntil --");
date.datesUntil(endExclusive, step).forEach(d -> {
    System.out.println(d);
});

// Result
// ↓
//-- datesUntil --
//2100-01-20
//2100-01-22
//2100-01-24
//2100-01-26
//2100-01-28
//2100-01-30
//2100-02-01
//2100-02-03
final var date = LocalDate.of(2100, 9, 20);
System.out.println(date); // 2100-09-20

final var endExclusive = LocalDate.of(2101, 2, 20);
System.out.println(endExclusive); // 2101-02-20

final var step = Period.ofMonths(1);
System.out.println(step); // P1M

System.out.println("-- datesUntil --");
date.datesUntil(endExclusive, step).forEach(d -> {
    System.out.println(d);
});

// Result
// ↓
//-- datesUntil --
//2100-09-20
//2100-10-20
//2100-11-20
//2100-12-20
//2101-01-20
final var date = LocalDate.of(2100, 9, 20);
System.out.println(date); // 2100-09-20

final var endExclusive = LocalDate.of(2120, 9, 20);
System.out.println(endExclusive); // 2120-09-20

final var step = Period.ofYears(4);
System.out.println(step); // P4Y

System.out.println("-- datesUntil --");
date.datesUntil(endExclusive, step).forEach(d -> {
    System.out.println(d);
});

// Result
// ↓
//-- datesUntil --
//2100-09-20
//2104-09-20
//2108-09-20
//2112-09-20
//2116-09-20

boolean equals (Object obj)

Checks if this date is equal to another date.

final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01

final var date2 = LocalDate.of(2100, 1, 1);
System.out.println(date2); // 2100-01-01

System.out.println(date1.equals(date2)); // true
final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01

final var date2 = LocalDate.of(2100, 1, 2);
System.out.println(date2); // 2100-01-02

System.out.println(date1.equals(date2)); // false
final var date1 = LocalDate.of(-2000, 1, 1);
System.out.println(date1); // -2000-01-01

final var date2 = LocalDate.of(2000, 1, 1);
System.out.println(date2); // 2000-01-01

System.out.println(date1.equals(date2)); // false

String format (DateTimeFormatter formatter)

Formats this date using the specified formatter.

final var date = LocalDate.of(2100, 12, 30);
System.out.println(date); // 2100-12-30

{
    final var ret1 = date.format(DateTimeFormatter.ISO_DATE);
    System.out.println(ret1); // 2100-12-30

    final var ret2 = date.format(DateTimeFormatter.BASIC_ISO_DATE);
    System.out.println(ret2); // 21001230
}
{
    System.out.println(Locale.getDefault().toLanguageTag()); // en-US

    final var ret1 = date.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL));
    System.out.println(ret1); // Thursday, December 30, 2100

    final var ret2 = date.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG));
    System.out.println(ret2); // December 30, 2100

    final var ret3 = date.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
    System.out.println(ret3); // Dec 30, 2100

    final var ret4 = date.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT));
    System.out.println(ret4); // 12/30/00
}

static LocalDate from (TemporalAccessor temporal)

Obtains an instance of LocalDate from a temporal object.

final var temporal = LocalDateTime.of(2100, 1, 3, 12, 30, 55);
System.out.println(temporal); // 2100-01-03T12:30:55
System.out.println(LocalDate.from(temporal)); // 2100-01-03
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles

final var temporal = ZonedDateTime.of(2100, 2, 4, 0, 0, 0, 0, zone);
System.out.println(temporal); // 2100-02-04T00:00-08:00[America/Los_Angeles]
System.out.println(LocalDate.from(temporal)); // 2100-02-04
final var offset = ZoneOffset.ofHours(9);
System.out.println(offset); // +09:00

final var temporal = OffsetDateTime.of(1800, 12, 1, 22, 30, 55, 0, offset);
System.out.println(temporal); // 1800-12-01T22:30:55+09:00
System.out.println(LocalDate.from(temporal)); // 1800-12-01

int get (TemporalField field)

Gets the value of the specified field from this date as an int.

final var date = LocalDate.of(2100, 4, 7);
System.out.println(date); // 2100-04-07

System.out.println(date.isSupported(ChronoField.YEAR)); // true
System.out.println(date.get(ChronoField.YEAR)); // 2100

System.out.println(date.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(date.get(ChronoField.MONTH_OF_YEAR)); // 4

System.out.println(date.isSupported(ChronoField.DAY_OF_MONTH)); // true
System.out.println(date.get(ChronoField.DAY_OF_MONTH)); // 7
final var date = LocalDate.of(2100, 1, 1);
System.out.println(date); // 2100-01-01

System.out.println(date.isSupported(ChronoField.HOUR_OF_DAY)); // false

try {
    final var ret = date.get(ChronoField.HOUR_OF_DAY);
} catch (UnsupportedTemporalTypeException e) {
    System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}

// Result
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: HourOfDay

IsoChronology getChronology ()

Gets the chronology of this date, which is the ISO calendar system.

final var date = LocalDate.of(2100, 4, 7);
System.out.println(date); // 2100-04-07

final var chronology = date.getChronology();
System.out.println(chronology); // ISO

int getDayOfMonth ()

Gets the day-of-month field.

final var date = LocalDate.of(2100, 1, 1);
System.out.println(date); // 2100-01-01
System.out.println(date.getDayOfMonth()); // 1
final var date = LocalDate.of(2100, 1, 2);
System.out.println(date); // 2100-01-02
System.out.println(date.getDayOfMonth()); // 2
final var date = LocalDate.of(2100, 1, 15);
System.out.println(date); // 2100-01-15
System.out.println(date.getDayOfMonth()); // 15
final var date = LocalDate.of(2100, 1, 30);
System.out.println(date); // 2100-01-30
System.out.println(date.getDayOfMonth()); // 30
final var date = LocalDate.of(2100, 1, 31);
System.out.println(date); // 2100-01-31
System.out.println(date.getDayOfMonth()); // 31
final var date = LocalDate.of(2100, 2, 1);
System.out.println(date); // 2100-02-01
System.out.println(date.getDayOfMonth()); // 1

DayOfWeek getDayOfWeek ()

Gets the day-of-week field, which is an enum DayOfWeek.

final var date = LocalDate.of(2100, 1, 3);
System.out.println(date); // 2100-01-03
System.out.println(date.getDayOfWeek()); // SUNDAY
final var date = LocalDate.of(2100, 1, 4);
System.out.println(date); // 2100-01-04
System.out.println(date.getDayOfWeek()); // MONDAY
final var date = LocalDate.of(2100, 1, 5);
System.out.println(date); // 2100-01-05
System.out.println(date.getDayOfWeek()); // TUESDAY
final var date = LocalDate.of(2100, 1, 6);
System.out.println(date); // 2100-01-06
System.out.println(date.getDayOfWeek()); // WEDNESDAY
final var date = LocalDate.of(2100, 1, 7);
System.out.println(date); // 2100-01-07
System.out.println(date.getDayOfWeek()); // THURSDAY
final var date = LocalDate.of(2100, 1, 8);
System.out.println(date); // 2100-01-08
System.out.println(date.getDayOfWeek()); // FRIDAY
final var date = LocalDate.of(2100, 1, 9);
System.out.println(date); // 2100-01-09
System.out.println(date.getDayOfWeek()); // SATURDAY
final var date = LocalDate.of(2100, 1, 10);
System.out.println(date); // 2100-01-10
System.out.println(date.getDayOfWeek()); // SUNDAY

int getDayOfYear ()

Gets the day-of-year field.

final var date = LocalDate.of(2100, 1, 1);
System.out.println(date); // 2100-01-01
System.out.println(date.getDayOfYear()); // 1
final var date = LocalDate.of(2100, 1, 2);
System.out.println(date); // 2100-01-02
System.out.println(date.getDayOfYear()); // 2
final var date = LocalDate.of(2100, 1, 30);
System.out.println(date); // 2100-01-30
System.out.println(date.getDayOfYear()); // 30
final var date = LocalDate.of(2100, 1, 31);
System.out.println(date); // 2100-01-31
System.out.println(date.getDayOfYear()); // 31
final var date = LocalDate.of(2100, 2, 1);
System.out.println(date); // 2100-02-01
System.out.println(date.getDayOfYear()); // 32
final var date = LocalDate.of(2100, 2, 2);
System.out.println(date); // 2100-02-02
System.out.println(date.getDayOfYear()); // 33
final var date = LocalDate.of(2100, 12, 31);
System.out.println(date); // 2100-12-31
System.out.println(date.getDayOfYear()); // 365
final var date = LocalDate.of(2101, 1, 1);
System.out.println(date); // 2101-01-01
System.out.println(date.getDayOfYear()); // 1

IsoEra getEra ()

Gets the era applicable at this date.

final var date = LocalDate.of(2100, 4, 7);
System.out.println(date); // 2100-04-07
System.out.println(date.getEra()); // CE

long getLong (TemporalField field)

Gets the value of the specified field from this date as a long.

final var date = LocalDate.of(2100, 4, 7);

System.out.println(date.getLong(ChronoField.YEAR)); // 2100
System.out.println(date.getLong(ChronoField.MONTH_OF_YEAR)); // 4
System.out.println(date.getLong(ChronoField.DAY_OF_MONTH)); // 7

Month getMonth ()

Gets the month-of-year field using the Month enum.

final var date = LocalDate.of(2100, 1, 15);
System.out.println(date); // 2100-01-15
System.out.println(date.getMonth()); // JANUARY
final var date = LocalDate.of(2100, 2, 15);
System.out.println(date); // 2100-02-15
System.out.println(date.getMonth()); // FEBRUARY
final var date = LocalDate.of(2100, 3, 15);
System.out.println(date); // 2100-03-15
System.out.println(date.getMonth()); // MARCH
final var date = LocalDate.of(2100, 12, 15);
System.out.println(date); // 2100-12-15
System.out.println(date.getMonth()); // DECEMBER
final var date = LocalDate.of(2101, 1, 1);
System.out.println(date); // 2101-01-01
System.out.println(date.getMonth()); // JANUARY

int getMonthValue ()

Gets the month-of-year field from 1 to 12.

final var date = LocalDate.of(2100, 1, 15);
System.out.println(date); // 2100-01-15
System.out.println(date.getMonthValue()); // 1
final var date = LocalDate.of(2100, 2, 15);
System.out.println(date); // 2100-02-15
System.out.println(date.getMonthValue()); // 2
final var date = LocalDate.of(2100, 3, 15);
System.out.println(date); // 2100-03-15
System.out.println(date.getMonthValue()); // 3
final var date = LocalDate.of(2100, 12, 15);
System.out.println(date); // 2100-12-15
System.out.println(date.getMonthValue()); // 12
final var date = LocalDate.of(2101, 1, 1);
System.out.println(date); // 2101-01-01
System.out.println(date.getMonthValue()); // 1

int getYear ()

Gets the year field.

final var date = LocalDate.of(2099, 1, 1);
System.out.println(date); // 2099-01-01
System.out.println(date.getYear()); // 2099
final var date = LocalDate.of(2100, 1, 1);
System.out.println(date); // 2100-01-01
System.out.println(date.getYear()); // 2100
final var date = LocalDate.of(1800, 1, 1);
System.out.println(date); // 1800-01-01
System.out.println(date.getYear()); // 1800

int hashCode ()

A hash code for this date.

System.out.println(LocalDate.of(1970, 1, 1).hashCode()); // 4034625
System.out.println(LocalDate.of(1970, 1, 2).hashCode()); // 4034626
System.out.println(LocalDate.of(2100, 4, 8).hashCode()); // 4303112
System.out.println(LocalDate.of(9999, 1, 1).hashCode()); // 20469825

boolean isAfter (ChronoLocalDate other)

Checks if this date is after the specified date.

final var date = LocalDate.of(2100, 4, 8);
System.out.println(date); // 2100-04-08

final var date2 = LocalDate.of(2100, 4, 7);
final var date3 = LocalDate.of(2100, 4, 8);
final var date4 = LocalDate.of(2100, 4, 9);

System.out.println(date2); // 2100-04-07
System.out.println(date3); // 2100-04-08
System.out.println(date4); // 2100-04-09

System.out.println(date.isAfter(date2)); // true
System.out.println(date.isAfter(date3)); // false
System.out.println(date.isAfter(date4)); // false
final var date = LocalDate.of(2100, 1, 1);
System.out.println(date); // 2100-01-01

final var date2 = LocalDate.of(2099, 12, 31);
final var date3 = LocalDate.of(3000, 1, 1);
final var date4 = LocalDate.of(-200, 1, 1);

System.out.println(date2); // 2099-12-31
System.out.println(date3); // 3000-01-01
System.out.println(date4); // -0200-01-01

System.out.println(date.isAfter(date2)); // true
System.out.println(date.isAfter(date3)); // false
System.out.println(date.isAfter(date4)); // true

boolean isBefore (ChronoLocalDate other)

Checks if this date is before the specified date.

final var date = LocalDate.of(2100, 4, 8);
System.out.println(date); // 2100-04-08

final var date2 = LocalDate.of(2100, 4, 7);
final var date3 = LocalDate.of(2100, 4, 8);
final var date4 = LocalDate.of(2100, 4, 9);

System.out.println(date2); // 2100-04-07
System.out.println(date3); // 2100-04-08
System.out.println(date4); // 2100-04-09

System.out.println(date.isBefore(date2)); // false
System.out.println(date.isBefore(date3)); // false
System.out.println(date.isBefore(date4)); // true
final var date = LocalDate.of(2100, 1, 1);
System.out.println(date); // 2100-01-01

final var date2 = LocalDate.of(2099, 12, 31);
final var date3 = LocalDate.of(3000, 1, 1);
final var date4 = LocalDate.of(-200, 1, 1);

System.out.println(date2); // 2099-12-31
System.out.println(date3); // 3000-01-01
System.out.println(date4); // -0200-01-01

System.out.println(date.isBefore(date2)); // false
System.out.println(date.isBefore(date3)); // true
System.out.println(date.isBefore(date4)); // false

boolean isEqual (ChronoLocalDate other)

Checks if this date is equal to the specified date.

final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01

final var date2 = LocalDate.of(2100, 1, 1);
System.out.println(date2); // 2100-01-01

System.out.println(date1.isEqual(date2)); // true
final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01

final var date2 = LocalDate.of(2100, 1, 2);
System.out.println(date2); // 2100-01-02

System.out.println(date1.isEqual(date2)); // false
final var date1 = LocalDate.of(2100, 2, 1);
System.out.println(date1); // 2100-02-01

final var date2 = LocalDate.of(2100, 1, 2);
System.out.println(date2); // 2100-01-02

System.out.println(date1.isEqual(date2)); // false

boolean isLeapYear ()

Checks if the year is a leap year, according to the ISO proleptic calendar system rules.

final var date1 = LocalDate.of(2104, 2, 29);
System.out.println(date1); // 2104-02-29
System.out.println(date1.isLeapYear()); // ture

final var date2 = LocalDate.of(2104, 12, 31);
System.out.println(date2); // 2104-12-31
System.out.println(date2.isLeapYear()); // true

final var date3 = LocalDate.of(2105, 1, 1);
System.out.println(date3); // 2105-01-01
System.out.println(date3.isLeapYear()); // false

boolean isSupported (TemporalField field)

Checks if the specified field is supported.

final var date = LocalDate.of(2100, 4, 7);
System.out.println(date); // 2100-04-07

System.out.println(date.isSupported(ChronoField.YEAR)); // true
System.out.println(date.get(ChronoField.YEAR)); // 2100

System.out.println(date.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(date.get(ChronoField.MONTH_OF_YEAR)); // 4

System.out.println(date.isSupported(ChronoField.DAY_OF_MONTH)); // true
System.out.println(date.get(ChronoField.DAY_OF_MONTH)); // 7
final var date = LocalDate.of(2100, 1, 1);
System.out.println(date); // 2100-01-01

System.out.println(date.isSupported(ChronoField.HOUR_OF_DAY)); // false

try {
    final var ret = date.get(ChronoField.HOUR_OF_DAY);
} catch (UnsupportedTemporalTypeException e) {
    System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}

// Result
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: HourOfDay

boolean isSupported (TemporalUnit unit)

Checks if the specified unit is supported.

final var date = LocalDate.of(2100, 4, 7);
System.out.println(date); // 2100-04-07

System.out.println(date.isSupported(ChronoUnit.DAYS)); // true
System.out.println(date.isSupported(ChronoUnit.WEEKS)); // true
System.out.println(date.isSupported(ChronoUnit.MONTHS)); // true
System.out.println(date.isSupported(ChronoUnit.YEARS)); // true

System.out.println(date.isSupported(ChronoUnit.HOURS)); // false

int lengthOfMonth ()

Returns the length of the month represented by this date.

final var date = LocalDate.of(2103, 1, 1);
System.out.println(date.lengthOfMonth()); // 31
final var date1 = LocalDate.of(2103, 2, 1);
System.out.println(date1.isLeapYear()); // false
System.out.println(date1.lengthOfMonth()); // 28

final var date2 = LocalDate.of(2104, 2, 1);
System.out.println(date2.isLeapYear()); // true
System.out.println(date2.lengthOfMonth()); // 29
final var date = LocalDate.of(2103, 4, 1);
System.out.println(date.lengthOfMonth()); // 30
final var date = LocalDate.of(2103, 12, 1);
System.out.println(date.lengthOfMonth()); // 31

int lengthOfYear ()

Returns the length of the year represented by this date.

final var date = LocalDate.of(2103, 1, 1);
System.out.println(date.lengthOfYear()); // 365
System.out.println(date.isLeapYear()); // false
final var date = LocalDate.of(2104, 1, 1);
System.out.println(date.lengthOfYear()); // 366
System.out.println(date.isLeapYear()); // true

LocalDate minus (long amountToSubtract, TemporalUnit unit)

Returns a copy of this date with the specified amount subtracted.

final var date = LocalDate.of(2100, 5, 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

LocalDate minus (TemporalAmount amountToSubtract)

Returns a copy of this date with the specified amount subtracted.

final var date = LocalDate.of(2100, 5, 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

LocalDate minusDays (long daysToSubtract)

Returns a copy of this LocalDate with the specified number of days subtracted.

final var date = LocalDate.of(2100, 5, 20);
System.out.println(date); // 2100-05-20

System.out.println(date.minusDays(1)); // 2100-05-19
System.out.println(date.minusDays(2)); // 2100-05-18
System.out.println(date.minusDays(20)); // 2100-04-30
System.out.println(date.minusDays(365)); // 2099-05-20

LocalDate minusMonths (long monthsToSubtract)

Returns a copy of this LocalDate with the specified number of months subtracted.

final var date = LocalDate.of(2100, 5, 20);
System.out.println(date); // 2100-05-20

System.out.println(date.minusMonths(1)); // 2100-04-20
System.out.println(date.minusMonths(2)); // 2100-03-20
System.out.println(date.minusMonths(12)); // 2099-05-20
final var date = LocalDate.of(2100, 3, 31);
System.out.println(date); // 2100-03-31
System.out.println(date.minusMonths(1)); // 2100-02-28

LocalDate minusWeeks (long weeksToSubtract)

Returns a copy of this LocalDate with the specified number of weeks subtracted.

final var date = LocalDate.of(2100, 5, 20);
System.out.println(date); // 2100-05-20

System.out.println(date.minusWeeks(1)); // 2100-05-13
System.out.println(date.minusWeeks(2)); // 2100-05-06
System.out.println(date.minusWeeks(20)); // 2099-12-31
System.out.println(date.minusWeeks(100)); // 2098-06-19

LocalDate minusYears (long yearsToSubtract)

Returns a copy of this LocalDate with the specified number of years subtracted.

final var date = LocalDate.of(2103, 5, 20);
System.out.println(date); // 2103-05-20

System.out.println(date.minusYears(1)); // 2102-05-20
System.out.println(date.minusYears(2)); // 2101-05-20
System.out.println(date.minusYears(10)); // 2093-05-20
final var date = LocalDate.of(2104, 2, 29);
System.out.println(date); // 2104-02-29
System.out.println(date.isLeapYear()); // true

System.out.println(date.minusYears(1)); // 2103-02-28

static LocalDate now ()

Obtains the current date from the system clock in the default time-zone.

final var date = LocalDate.now();
System.out.println(date); // 2024-05-09

static LocalDate now (Clock clock)

Obtains the current date from the specified clock.

// A clock advanced 5 days.
final var clock = Clock.offset(Clock.systemDefaultZone(), Duration.ofDays(5));

System.out.println(LocalDate.now()); // 2024-05-09
System.out.println(LocalDate.now(clock)); // 2024-05-14

static LocalDate now (ZoneId zone)

Obtains the current date from the system clock in the specified time-zone.

final var zone1 = ZoneId.systemDefault();
System.out.println(zone1); // America/Los_Angeles

final var zone2 = ZoneId.of("Asia/Tokyo");
System.out.println(zone2); // Asia/Tokyo

System.out.println(LocalDate.now(zone1)); // 2024-05-10
System.out.println(LocalDate.now(zone2)); // 2024-05-11

static LocalDate of (int year, int month, int dayOfMonth)

Obtains an instance of LocalDate from a year, month and day.

System.out.println(LocalDate.of(2100, 2, 15)); // 2100-02-15
System.out.println(LocalDate.of(1800, 4, 8)); // 1800-04-08

static LocalDate of (int year, Month month, int dayOfMonth)

Obtains an instance of LocalDate from a year, month and day.

System.out.println(LocalDate.of(2100, Month.FEBRUARY, 15)); // 2100-02-15
System.out.println(LocalDate.of(1800, Month.APRIL, 8)); // 1800-04-08

static LocalDate ofEpochDay (long epochDay)

Obtains an instance of LocalDate from the epoch day count.

System.out.println(LocalDate.ofEpochDay(0)); // 1970-01-01
System.out.println(LocalDate.ofEpochDay(1)); // 1970-01-02
System.out.println(LocalDate.ofEpochDay(2)); // 1970-01-03
System.out.println(LocalDate.ofEpochDay(50000)); // 2106-11-24

static LocalDate ofInstant (Instant instant, ZoneId zone)

Obtains an instance of LocalDate from an Instant and zone ID.

final var instant = Instant.ofEpochSecond(4102488000L);
System.out.println(instant); // 2100-01-01T12:00:00Z

final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
System.out.println(LocalDate.ofInstant(instant, zone)); // 2100-01-01

System.out.println(LocalDate.ofInstant(instant, ZoneOffset.UTC)); // 2100-01-01
System.out.println(LocalDate.ofInstant(instant, ZoneOffset.ofHours(13))); // 2100-01-02
System.out.println(LocalDate.ofInstant(instant, ZoneOffset.ofHours(-13))); // 2099-12-31

static LocalDate ofYearDay (int year, int dayOfYear)

Obtains an instance of LocalDate from a year and day-of-year.

System.out.println(LocalDate.ofYearDay(2100, 1)); // 2100-01-01
System.out.println(LocalDate.ofYearDay(2100, 2)); // 2100-01-02
System.out.println(LocalDate.ofYearDay(2100, 31)); // 2100-01-31
System.out.println(LocalDate.ofYearDay(2100, 32)); // 2100-02-01
System.out.println(LocalDate.ofYearDay(2100, 365)); // 2100-12-31

static LocalDate parse (CharSequence text)

Obtains an instance of LocalDate from a text string such as 2007-12-03.

final var date1 = LocalDate.parse("2100-04-08");
System.out.println(date1); // 2100-04-08

final var date2 = LocalDate.parse("1800-12-31");
System.out.println(date2); // 1800-12-31
try {
    final var ret = LocalDate.parse("2100/04/03");
} catch (DateTimeParseException e) {
    System.out.println("DateTimeParseException! : " + e.getMessage());
}

// Result
// ↓
//DateTimeParseException! : Text '2100/04/03' could not be parsed at index 4

static LocalDate parse (CharSequence text, DateTimeFormatter formatter)

Obtains an instance of LocalDate from a text string using a specific formatter.

final var date = LocalDate.parse("2100-04-08",
        DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(date); // 2100-04-08
final var date = LocalDate.parse("12/31/2100",
        DateTimeFormatter.ofPattern("M/d/y"));
System.out.println(date); // 2100-12-31
final var date = LocalDate.parse("April 7, 1800",
        DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG));
System.out.println(date); // 1800-04-07

LocalDate plus (long amountToAdd, TemporalUnit unit)

Returns a copy of this date with the specified amount added.

final var date = LocalDate.of(2100, 5, 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

LocalDate plus (TemporalAmount amountToAdd)

Returns a copy of this date with the specified amount added.

final var date = LocalDate.of(2100, 5, 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

LocalDate plusDays (long daysToAdd)

Returns a copy of this LocalDate with the specified number of days added.

final var date = LocalDate.of(2100, 5, 20);
System.out.println(date); // 2100-05-20

System.out.println(date.plusDays(1)); // 2100-05-21
System.out.println(date.plusDays(2)); // 2100-05-22
System.out.println(date.plusDays(20)); // 2100-06-09
System.out.println(date.plusDays(365)); // 2101-05-20

LocalDate plusMonths (long monthsToAdd)

Returns a copy of this LocalDate with the specified number of months added.

final var date = LocalDate.of(2100, 5, 20);
System.out.println(date); // 2100-05-20

System.out.println(date.plusMonths(1)); // 2100-06-20
System.out.println(date.plusMonths(2)); // 2100-07-20
System.out.println(date.plusMonths(12)); // 2101-05-20
final var date = LocalDate.of(2100, 3, 31);
System.out.println(date); // 2100-03-31
System.out.println(date.plusMonths(1)); // 2100-04-30

LocalDate plusWeeks (long weeksToAdd)

Returns a copy of this LocalDate with the specified number of weeks added.

final var date = LocalDate.of(2100, 5, 20);
System.out.println(date); // 2100-05-20

System.out.println(date.plusWeeks(1)); // 2100-05-27
System.out.println(date.plusWeeks(2)); // 2100-06-03
System.out.println(date.plusWeeks(20)); // 2100-10-07
System.out.println(date.plusWeeks(100)); // 2102-04-20

LocalDate plusYears (long yearsToAdd)

Returns a copy of this LocalDate with the specified number of years added.

final var date = LocalDate.of(2103, 5, 20);
System.out.println(date); // 2103-05-20

System.out.println(date.plusYears(1)); // 2104-05-20
System.out.println(date.plusYears(2)); // 2105-05-20
System.out.println(date.plusYears(10)); // 2113-05-20
final var date = LocalDate.of(2104, 2, 29);
System.out.println(date); // 2104-02-29
System.out.println(date.isLeapYear()); // true

System.out.println(date.plusYears(1)); // 2105-02-28

<R> R query (TemporalQuery<R> query)

Queries this date using the specified query.

final var date = LocalDate.of(2100, 4, 8);
System.out.println(date); // 2100-04-08
System.out.println(date.query(TemporalQueries.precision())); // Days

ValueRange range (TemporalField field)

Gets the range of valid values for the specified field.

final var date = LocalDate.of(2103, 4, 8);
System.out.println(date); // 2103-04-08
System.out.println(date.range(ChronoField.YEAR)); // -999999999 - 999999999
System.out.println(date.range(ChronoField.MONTH_OF_YEAR)); // 1 - 12
System.out.println(date.range(ChronoField.DAY_OF_MONTH)); // 1 - 30
final var date1 = LocalDate.of(2103, 2, 1);
System.out.println(date1); // 2103-02-01
System.out.println(date1.isLeapYear()); // false
System.out.println(date1.range(ChronoField.DAY_OF_MONTH)); // 1 - 28

final var date2 = LocalDate.of(2104, 2, 1);
System.out.println(date2); // 2104-02-01
System.out.println(date2.isLeapYear()); // true
System.out.println(date2.range(ChronoField.DAY_OF_MONTH)); // 1 - 29

long toEpochSecond (LocalTime time, ZoneOffset offset)

Converts this LocalDate to the number of seconds since the epoch of 1970-01-01T00:00:00Z.

final var date = LocalDate.of(2100, 4, 8);
System.out.println(date); // 2100-04-08

final var time = LocalTime.of(12, 30);
System.out.println(time); // 12:30

final long epochSecond1 = date.toEpochSecond(time, ZoneOffset.UTC);
System.out.println(epochSecond1); // 4110870600
System.out.println(Instant.ofEpochSecond(epochSecond1)); // 2100-04-08T12:30:00Z

final long epochSecond2 = date.toEpochSecond(time, ZoneOffset.ofHours(8));
System.out.println(epochSecond2); // 4110841800
System.out.println(Instant.ofEpochSecond(epochSecond2)); // 2100-04-08T04:30:00Z

String toString ()

Outputs this date as a String, such as 2007-12-03.

final var ret1 = LocalDate.of(2100, 4, 8).toString();
System.out.println(ret1); // 2100-04-08

final var ret2 = LocalDate.MAX.toString();
System.out.println(ret2); // +999999999-12-31

final var ret3 = LocalDate.MIN.toString();
System.out.println(ret3); // -999999999-01-01

Period until (ChronoLocalDate endDateExclusive)

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

final var date1 = LocalDate.of(2100, 1, 15);
final var date2 = LocalDate.of(2101, 3, 18);

final var period1 = date1.until(date2);
System.out.println(period1.getYears()); // 1
System.out.println(period1.getMonths()); // 2
System.out.println(period1.getDays()); // 3

final var period2 = date2.until(date1);
System.out.println(period2.getYears()); // -1
System.out.println(period2.getMonths()); // -2
System.out.println(period2.getDays()); // -3
final var date1 = LocalDate.of(2100, 2, 2);
final var date2 = LocalDate.of(2100, 3, 1);

final var period = date1.until(date2);
System.out.println(period.getYears()); // 0
System.out.println(period.getMonths()); // 0
System.out.println(period.getDays()); // 27
final var date1 = LocalDate.of(2100, 2, 1);
final var date2 = LocalDate.of(2100, 3, 1);

final var period = date1.until(date2);
System.out.println(period.getYears()); // 0
System.out.println(period.getMonths()); // 1
System.out.println(period.getDays()); // 0

long until (Temporal endExclusive, TemporalUnit unit)

Calculates the amount of time until another date in terms of the specified unit.

final var date1 = LocalDate.of(2100, 1, 15);
final var date2 = LocalDate.of(2101, 3, 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 var date1 = LocalDate.of(2100, 6, 15);
final var date2 = LocalDate.of(2100, 8, 14);

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)); // 60

LocalDate with (TemporalAdjuster adjuster)

Returns an adjusted copy of this date.

final var date = LocalDate.of(2100, 4, 8);
System.out.println(date); // 2100-04-08

System.out.println(date.with(TemporalAdjusters.firstDayOfMonth())); // 2100-04-01
System.out.println(date.with(TemporalAdjusters.lastDayOfMonth())); // 2100-04-30

System.out.println(date.with(Year.of(1800))); // 1800-04-08
System.out.println(date.with(Month.JANUARY)); // 2100-01-08

LocalDate with (TemporalField field, long newValue)

Returns a copy of this date with the specified field set to a new value.

final var date = LocalDate.of(2100, 4, 8);
System.out.println(date); // 2100-04-08

System.out.println(date.with(ChronoField.YEAR, 1800)); // 1800-04-08
System.out.println(date.with(ChronoField.MONTH_OF_YEAR, 12)); // 2100-12-08
System.out.println(date.with(ChronoField.DAY_OF_MONTH, 30)); // 2100-04-30

LocalDate withDayOfMonth (int dayOfMonth)

Returns a copy of this LocalDate with the day-of-month altered.

final var date = LocalDate.of(2100, 4, 8);
System.out.println(date); // 2100-04-08

System.out.println(date.withDayOfMonth(1)); // 2100-04-01
System.out.println(date.withDayOfMonth(2)); // 2100-04-02
System.out.println(date.withDayOfMonth(29)); // 2100-04-29
System.out.println(date.withDayOfMonth(30)); // 2100-04-30

LocalDate withDayOfYear (int dayOfYear)

Returns a copy of this LocalDate with the day-of-year altered.

final var date = LocalDate.of(2100, 4, 8);
System.out.println(date); // 2100-04-08

System.out.println(date.withDayOfYear(1)); // 2100-01-01
System.out.println(date.withDayOfYear(2)); // 2100-01-02
System.out.println(date.withDayOfYear(30)); // 2100-01-30
System.out.println(date.withDayOfYear(31)); // 2100-01-31
System.out.println(date.withDayOfYear(32)); // 2100-02-01
System.out.println(date.withDayOfYear(33)); // 2100-02-02

System.out.println(date.withDayOfYear(365)); // 2100-12-31

LocalDate withMonth (int month)

Returns a copy of this LocalDate with the month-of-year altered.

final var date = LocalDate.of(2100, 4, 8);
System.out.println(date); // 2100-04-08

System.out.println(date.withMonth(1)); // 2100-01-08
System.out.println(date.withMonth(2)); // 2100-02-08
System.out.println(date.withMonth(11)); // 2100-11-08
System.out.println(date.withMonth(12)); // 2100-12-08

LocalDate withYear (int year)

Returns a copy of this LocalDate with the year altered.

final var date = LocalDate.of(2100, 4, 8);
System.out.println(date); // 2100-04-08

System.out.println(date.withYear(1800)); // 1800-04-08
System.out.println(date.withYear(-999)); // -0999-04-08
System.out.println(date.withYear(3000)); // 3000-04-08

Methods declared in ChronoLocalDate

toEpochDay

Please see the link below.


Related posts

To top of page