Java : LocalDate with Examples
LocalDate (Java SE 21 & JDK 21) with Examples.
You will find code examples on most LocalDate methods.
Summary
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
System.out.println(LocalDate.EPOCH); // 1970-01-01
static final LocalDate MAX
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
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)
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 ()
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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 ()
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 ()
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 ()
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 ()
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 ()
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)
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 ()
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 ()
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 ()
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 ()
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)
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)
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)
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 ()
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)
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)
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 ()
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 ()
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)
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)
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)
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)
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)
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)
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 ()
final var date = LocalDate.now();
System.out.println(date); // 2024-05-09
static LocalDate now (Clock 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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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 ()
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)
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)
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)
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)
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)
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)
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)
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)
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
Related posts
- Get the current time
- Basics of Date and Time
- Don't use the legacy Date and Calendar classes, use new APIs instead
- API Examples
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDateTime
- LocalTime
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset