mydomain
No ADS
No ADS

Java LocalDate Tutorial with Examples

  1. LocalDate
  2. Static Factory methods
  3. format(DateTimeFormatter)
  4. isBefore(..), isAfter(..)
  5. lengthOfMonth(), lengthOfYear()
  6. getX() *
  7. get(TemporalField)
  8. getLong(TemporalField)
  9. plusX(..) *
  10. plus(TemporalAmount)
  11. plus(long, TemporalUnit)
  12. minusX(..) *
  13. minus(TemporalAmount)
  14. minus(long, TemporalUnit)
  15. withX(..) *
  16. with(TemporalAdjuster)
  17. with(TemporalField, long)
  18. range(TemporalField)
  19. query(TemporalQuery<R>)
  20. isLeapYear()
  21. isSupported(TemporalField)
  22. isSupported(TemporalUnit)

1. LocalDate

The LocalDate class represents the local date in the ISO calendar system. It does not include time and time zone information. A LocalDate can be a date of birth, an official holiday, etc. it is related to a specific day of the year.
  • Java 8 Date Time API Overview
The LocalDate class is in the java.time package. Like other classes introduced in Java 8 Date Time API, LocalDate is immutable which means that all computations on LocalDate create a new LocalDate object, so it's safe to be used in multithreading environment.
public final class LocalDate
        implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable

2. Static Factory methods

No ADS
The LocalDate class does not provide any constructor, but it provides static factory methods to create a new object.
static LocalDate now()
static LocalDate now​(Clock clock)
static LocalDate now​(ZoneId zone)
static LocalDate of​(int year, int month, int dayOfMonth)
static LocalDate of​(int year, Month month, int dayOfMonth)
static LocalDate ofEpochDay​(long epochDay)
static LocalDate ofInstant​(Instant instant, ZoneId zone)
static LocalDate ofYearDay​(int year, int dayOfYear)
static LocalDate parse​(CharSequence text)
static LocalDate parse​(CharSequence text, DateTimeFormatter formatter)
Create a LocalDate object representing the current date from the system clock and default time zone (On your computer).
// Ex: Current date from the system clock in the default time-zone.
LocalDate localDate = LocalDate.now();
System.out.println("localDate.now(): " + localDate); // 2021-06-17
Create a LocalDate object representing the current date from a specified clock:
Clock clock = Clock.systemDefaultZone();

LocalDate localDate = LocalDate.now(clock);
System.out.println("localDate.now(clock): " + localDate);
  • Java Clock
Create a LocalDate object representing the current date from a specified ZoneId:
// Default system time-zone.
ZoneId zoneIdDefault = ZoneId.systemDefault();
        
// Ho Chi Minh City, Vietnam. UTC+7
ZoneId zoneId = ZoneId.of("Asia/Ho_Chi_Minh");

LocalDate localDate = LocalDate.now(zoneId);
System.out.println("localDate: " + localDate); // 2021-06-17
Create a LocalDate object from the specified year, month, dayOfMonth:
// Ex: LocalDate based on year, month, dayOfMonth.
LocalDate localDate1 = LocalDate.of(2000, 5, 20); // 2000-05-20
System.out.println("localDate1: " + localDate1);

LocalDate localDate2 = LocalDate.of(2000, Month.MAY, 20); // 2000-05-20
System.out.println("localDate2: " + localDate2);
Create a LocalDate object from the specified year, dayOfYear:
// Ex: LocalDate based on year and dayOfYear
LocalDate localDate1 = LocalDate.ofYearDay(1995, 1); // 1995-01-01 + 0 days.
System.out.println("localDate1: " + localDate1); // 1995-01-01

LocalDate localDate2 = LocalDate.ofYearDay(1995, 51); // 1995-01-01 + 50 days.
System.out.println("localDate2: " + localDate2); // 1995-02-20
Create a LocalDate object from the specified epochDay:
// Ex: LocalDate based on epochDay.
LocalDate localDate1 = LocalDate.ofEpochDay(0); // 1971-01-01 + 0 days.
System.out.println("localDate1: " + localDate1); // 1970-01-01

LocalDate localDate2 = LocalDate.ofEpochDay(1000); // 1971-01-01 + 1000 days.
System.out.println("localDate2: " + localDate2); // 1972-09-27
Create a LocalDate object from Instant and ZoneId objects:
Instant instant = Instant.now();
ZoneId zoneId = ZoneId.systemDefault();

// LocalDate based on Instant and ZoneId.
LocalDate localDate = LocalDate.ofInstant(instant, zoneId);
System.out.println("localDate: " + localDate);
Create a LocalDate object from parsing a date formatted text:
// Ex: LocalDate based on CharSequence.
LocalDate localDate1 = LocalDate.parse("2011-11-20");
System.out.println("localDate1: " + localDate1); // 2011-11-20

// Ex: LocalDate based on CharSequence and DateTimeFormatter
DateTimeFormatter fmt2 = DateTimeFormatter.ofPattern("dd/MM/yyyy");

LocalDate localDate2 = LocalDate.parse("20/11/2020", fmt2);
System.out.println("localDate2: " + localDate2); // 2011-11-20
  • Java Date Time Format Pattern

3. format(DateTimeFormatter)

No ADS
Format this LocalDate object with a specified DateTimeFormatter object.
// Inherited from ChronoLocalDate interface
public String format(DateTimeFormatter formatter)
Example:
LocalDate_format_ex1.java
LocalDate localDate = LocalDate.parse("2020-05-15"); // 2020-May-15
System.out.println("localDate: " + localDate); // 2020-05-15
System.out.println();

DateTimeFormatter fmt1 = DateTimeFormatter.BASIC_ISO_DATE; // yyyyMMdd  
System.out.println("BASIC_ISO_DATE: " + localDate.format(fmt1)); // 20200515

DateTimeFormatter fmt2 = DateTimeFormatter.ofPattern("dd/MM/yyyy");  
System.out.println("dd/MM/yyyy: " + localDate.format(fmt2)); // 15/05/2020
  • Java DateTimeFormatter
  • Java Date Time Format Pattern

4. isBefore(..), isAfter(..)

The isBefore(ChronoLocalDate) method is used to check if this LocalDate object is before another specified object on the timeline.
// Inherited from ChronoLocalDate interface
public boolean isBefore(ChronoLocalDate other)
The isAfter(ChronoLocalDate) method is used to check if this LocalDate object is after another specified object on the timeline.
// Inherited from ChronoLocalDate interface
public boolean isAfter(ChronoLocalDate other)
localDate1 is before localDate2.
LocalDate localDate1 = LocalDate.parse("1990-05-15"); // 1990-May-15
LocalDate localDate2 = LocalDate.parse("2020-12-15"); // 2020-Dec-15


System.out.println("localDate1: " + localDate1); // 1990-05-15
System.out.println("localDate2: " + localDate2); // 2020-12-15
System.out.println();

System.out.println("localDate1.isBefore(localDate2): " + localDate1.isBefore(localDate2)); // true
System.out.println("localDate1.isAfter(localDate2): " + localDate1.isAfter(localDate2)); // false
Output:
localDate1: 1990-05-15
localDate2: 2020-12-15

localDate1.isBefore(localDate2): true
localDate1.isAfter(localDate2): false
  • Java ChronoLocalDate

5. lengthOfMonth(), lengthOfYear()

The lengthOfMonth() method returns the number of days in the month represented by this LocalDate object.
The lengthOfYear() method returns the number of days in the year represented by this LocalDate object.
public int lengthOfMonth()  

public int lengthOfYear()
Example:
LocalDate_lengthX_ex1.java
package org.o7planning.localdate.ex;

import java.time.LocalDate;

public class LocalDate_lengthX_ex1 {

    public static void main(String[] args) {
        LocalDate localDate = LocalDate.parse("2020-05-15"); // 2020-May-15
        System.out.println("localDate: " + localDate); // 2020-05-15
        System.out.println();
        
        System.out.println("localDate.lengthOfMonth(): " + localDate.lengthOfMonth());  // 31 days
        System.out.println("localDate.lengthOfYear(): " + localDate.lengthOfYear()); // 366 days
    }
}

6. getX() *

No ADS
The getYear() method returns the year represented by this LocalDate. It is equivalent to calling the get(ChronoField.YEAR) method. If you want to get the year of the era, call the get(ChronoField. YEAR_OF_ERA).
public int getYear()  
public int getMonthValue()   
public Month getMonth()
public int getDayOfMonth()
public int getDayOfYear()
public DayOfWeek getDayOfWeek()  
public IsoEra getEra()  
public IsoChronology getChronology()
The other methods getMonthValue(), getMonth(), getDayOfMonth(), getDayOfYear(), getDayOfWeek(), ... are also understood as their names.
Method
Same As
getYear()
localDate.get(ChronoField.YEAR)
getMonthValue()
localDate.get(ChronoField.MONTH_OF_YEAR)
getMonth()
Month.of(localDate.get(ChronoField.MONTH_OF_YEAR))
getDayOfMonth()
localDate.get(ChronoField.DAY_OF_MONTH)
getDayOfYear()
localDate.get(ChronoField.DAY_OF_YEAR)
getDayOfWeek()
DayOfWeek.of(localDate.get(ChronoField.DAY_OF_WEEK))
getEra()
localDate.get(ChronoField.ERA)
getChronology()
localDate.get(ChronoField.CHRONOLOGY)
Example:
LocalDate_getX_ex1.java
LocalDate localDate = LocalDate.parse("2020-05-15"); // 2020-May-15
System.out.println("localDate: " + localDate); // 2020-05-15
System.out.println();

System.out.println("localDate.getEra(): " + localDate.getEra());  // IsoEra.CE  (enum)
System.out.println("localDate.getDayOfYear(): " + localDate.getDayOfYear()); // 136
System.out.println("localDate.getDayOfMonth(): " + localDate.getDayOfMonth()); // 15
System.out.println("localDate.getYear(): " + localDate.getYear());  // 2020
System.out.println("localDate.getMonth(): " + localDate.getMonth());  // Month.MAY (enum)
System.out.println("localDate.getMonthValue(): " + localDate.getMonthValue()); // 5
System.out.println("localDate.getDayOfWeek(): " + localDate.getDayOfWeek()); // DayOfWeek.FRIDAY (enum)
Output:
localDate: 2020-05-15

localDate.getEra(): CE
localDate.getDayOfYear(): 136
localDate.getDayOfMonth(): 15
localDate.getYear(): 2020
localDate.getMonth(): MAY
localDate.getMonthValue(): 5
localDate.getDayOfWeek(): FRIDAY

7. get(TemporalField)

Return the value of the specified field of this LocalDate object as a 32-bit integer.
// Inherited from TemporalAccessor interface
public int get(TemporalField field)
Note: Some fields may not be supported by LocalDate and an UnsupportedTemporalTypeException will be thrown. To be sure, use the isSupported(TemporalField) method to check if a certain field is supported by LocalDate.
Example:
LocalDate_get_field_ex1.java
LocalDate date = LocalDate.parse("2020-05-15"); // 2020-May-15
System.out.println("localDate: " + date); // 2020-05-15
System.out.println();

// int get(TemporalField field)
System.out.println("DAY_OF_MONTH: " + date.get(ChronoField.DAY_OF_MONTH)); // 15
System.out.println("DAY_OF_WEEK: " + date.get(ChronoField.DAY_OF_WEEK)); // 5
System.out.println("DAY_OF_YEAR: " + date.get(ChronoField.DAY_OF_YEAR)); // 136
System.out.println("ERA: " + date.get(ChronoField.ERA)); // 1
System.out.println("YEAR: " + date.get(ChronoField.YEAR)); // 2020
System.out.println("YEAR_OF_ERA: " + date.get(ChronoField.YEAR_OF_ERA)); // 2020
System.out.println("ALIGNED_DAY_OF_WEEK_IN_MONTH: " + date.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH)); // 1
System.out.println("ALIGNED_DAY_OF_WEEK_IN_YEAR: " + date.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR)); // 3
System.out.println("ALIGNED_WEEK_OF_MONTH: " + date.get(ChronoField.ALIGNED_WEEK_OF_MONTH)); // 3
System.out.println("ALIGNED_WEEK_OF_YEAR: " + date.get(ChronoField.ALIGNED_WEEK_OF_YEAR)); // 20

// Use getLong() instead
// System.out.println("PROLEPTIC_MONTH: " + date.get(ChronoField.PROLEPTIC_MONTH)); // 24244
// System.out.println("EPOCH_DAY: " + date.get(ChronoField.EPOCH_DAY)); // 18397
  • ChronoField
  • TemporalField

8. getLong(TemporalField)

No ADS
Return the value of the specified field of this LocalDate object as a 64-bit integer.
// Inherited from TemporalAccessor interface
public long getLong(TemporalField field)
Note: Some fields may not be supported by LocalDate and an UnsupportedTemporalTypeException will be thrown. To be sure, use the isSupported(TemporalField) method to check if a certain field is supported by LocalDate.
Example:
LocalDate_getLong_field_ex1.java
LocalDate date = LocalDate.parse("2020-05-15"); // 2020-May-15
System.out.println("localDate: " + date); // 2020-05-15
System.out.println();

// long getLong(TemporalField field)
System.out.println("DAY_OF_MONTH: " + date.getLong(ChronoField.DAY_OF_MONTH)); // 15
System.out.println("DAY_OF_WEEK: " + date.getLong(ChronoField.DAY_OF_WEEK)); // 5
System.out.println("DAY_OF_YEAR: " + date.getLong(ChronoField.DAY_OF_YEAR)); // 136
System.out.println("ERA: " + date.getLong(ChronoField.ERA)); // 1
System.out.println("YEAR: " + date.getLong(ChronoField.YEAR)); // 2020
System.out.println("YEAR_OF_ERA: " + date.getLong(ChronoField.YEAR_OF_ERA)); // 2020
System.out.println("ALIGNED_DAY_OF_WEEK_IN_MONTH: " + date.getLong(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH)); // 1
System.out.println("ALIGNED_DAY_OF_WEEK_IN_YEAR: " + date.getLong(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR)); // 3
System.out.println("ALIGNED_WEEK_OF_MONTH: " + date.getLong(ChronoField.ALIGNED_WEEK_OF_MONTH)); // 3
System.out.println("ALIGNED_WEEK_OF_YEAR: " + date.getLong(ChronoField.ALIGNED_WEEK_OF_YEAR)); // 20

System.out.println("PROLEPTIC_MONTH: " + date.getLong(ChronoField.PROLEPTIC_MONTH)); // 24244
System.out.println("EPOCH_DAY: " + date.getLong(ChronoField.EPOCH_DAY)); // 18397
  • TemporalField
  • ChronoField

9. plusX(..) *

No ADS
The plusYear(yearsToAdd) method returns a copy of this LocalDate object with the specified amount of years added.
The methods plusMonths(monthsToAdd), plusWeeks(weeksToAdd), plusDays(daysToAdd) are also understood as the names imply.
public LocalDate plusYears(long yearsToAdd)  
public LocalDate plusMonths(long monthsToAdd)  
public LocalDate plusWeeks(long weeksToAdd)
public LocalDate plusDays(long daysToAdd)
Example:
LocalDate_plusX_ex1.java
LocalDate localDate = LocalDate.parse("2020-05-15"); // 2020-May-15
System.out.println("localDate: " + localDate); // 2020-05-15
System.out.println();

// plusDays(long daysToAdd)
LocalDate localDate1 = localDate.plusDays(10);
System.out.println("localDate1: " + localDate1); // 2020-05-25

LocalDate localDate2 = localDate.plusDays(-15);
System.out.println("localDate2: " + localDate2); // 2020-04-30

// plusMonths(long monthsToAdd)
LocalDate localDate3 = localDate.plusMonths(3);
System.out.println("localDate3: " + localDate3); // 2020-08-15

// plusYears(long yearsToAdd)
LocalDate localDate4 = localDate.plusYears(1);
System.out.println("localDate4: " + localDate4); // 2021-05-15

// plusWeeks(long weeksToAdd)
LocalDate localDate5 = localDate.plusWeeks(2);
System.out.println("localDate5: " + localDate5); // 2020-05-29
Output:
localDate: 2020-05-15

localDate1: 2020-05-25
localDate2: 2020-04-30
localDate3: 2020-08-15
localDate4: 2021-05-15
localDate5: 2020-05-29

10. plus(TemporalAmount)

Return a copy of this LocalDate object with an added amount of time.
// Inherited from Temporal interface
public LocalDate plus(TemporalAmount amountToAdd)
Example:
LocalDate_plus_amount_ex1.java
LocalDate localDate = LocalDate.parse("2020-05-15"); // 2020-May-15
System.out.println("localDate: " + localDate); // 2020-05-15
System.out.println();

// plus(TemporalAmount amountToAdd)
TemporalAmount amount1 = Period.ofDays(5);
LocalDate localDate1 = localDate.plus(amount1);
System.out.println("localDate1: " + localDate1); // 2020-05-20

// plus(TemporalAmount amountToAdd)
LocalDate from = LocalDate.of(2020, 5, 10);
LocalDate to = LocalDate.of(2020, 5, 12);

TemporalAmount amount2 = Period.between(from, to); // 2 days
LocalDate localDate2 = localDate.plus(amount2);
System.out.println("localDate2: " + localDate2
Output:
localDate: 2020-05-15

localDate1: 2020-05-20
localDate2: 2020-05-17

11. plus(long, TemporalUnit)

No ADS
Return a copy of this LocalDate object with a specified value added in the given unit.
// Inherited from Temporal interface
public LocalDate plus(long amountToAdd, TemporalUnit unit)
Example:
LocalDate_plus_unit_ex1.java
LocalDate localDate = LocalDate.parse("2020-05-15"); // 2020-May-15
System.out.println("localDate: " + localDate); // 2020-05-15
System.out.println();

// plus(long amountToAdd, TemporalUnit unit)
LocalDate localDate1 = localDate.plus(5, ChronoUnit.DAYS);
System.out.println("localDate1: " + localDate1); // 2020-05-20

// plus(long amountToAdd, TemporalUnit unit)
LocalDate localDate2 = localDate.plus(10, ChronoUnit.MONTHS);
System.out.println("localDate2: " + localDate2); // 2021-03-15
Output:
localDate: 2020-05-15

localDate1: 2020-05-20
localDate2: 2021-03-15

12. minusX(..) *

The minusYears(yearsToSubtract) method returns a copy of this LocalDate object with the specified amount of years subtracted.
The methods minusMonths(monthsToSubtract), minusWeeks(weeksToSubtract), minusDays(daysToSubtract) is also understood as their names.
public LocalDate minusYears(long yearsToSubtract)  
public LocalDate minusMonths(long monthsToSubtract)  
public LocalDate minusWeeks(long weeksToSubtract)  
public LocalDate minusDays(long daysToSubtract)
Example:
LocalDate localDate = LocalDate.parse("2020-05-15"); // 2020-May-15
System.out.println("localDate: " + localDate); // 2020-05-15
System.out.println();

// minusDays(long daysToSubtract)
LocalDate localDate1 = localDate.minusDays(10);
System.out.println("localDate1: " + localDate1); // 2020-05-05

LocalDate localDate2 = localDate.minusDays(-10);
System.out.println("localDate2: " + localDate2); // 2020-05-25

// minusMonths(long monthsToSubtract)
LocalDate localDate3 = localDate.minusMonths(3);
System.out.println("localDate3: " + localDate3); // 2020-02-15

// minusYears(long yearsToSubtract)
LocalDate localDate4 = localDate.minusYears(1);
System.out.println("localDate4: " + localDate4); // 2019-05-15

// minusWeeks(long weeksToSubtract)
LocalDate localDate5 = localDate.minusWeeks(2);
System.out.println("localDate5: " + localDate5); // 2020-05-01

// minus(TemporalAmount)
TemporalAmount amount6 = Period.ofDays(5);
LocalDate localDate6 = localDate.minus(amount6);
System.out.println("localDate6: " + localDate6); // 2020-05-10

// minus(TemporalAmount)
LocalDate from = LocalDate.of(2020, 5, 10);
LocalDate to = LocalDate.of(2020, 5, 12);

TemporalAmount amount7 = Period.between(from, to); // 2 days
LocalDate localDate7 = localDate.minus(amount7);
System.out.println("localDate7: " + localDate7); // 2020-05-13

// minus(long amountToSubtract, TemporalUnit unit)
TemporalUnit unit = ChronoUnit.DAYS;
LocalDate localDate8 = localDate.minus(3, unit);

System.out.println("localDate8: " + localDate8); // 2020-05-12
Output:
localDate: 2020-05-15

localDate1: 2020-05-05
localDate2: 2020-05-25
localDate3: 2020-02-15
localDate4: 2019-05-15
localDate5: 2020-05-01
localDate6: 2020-05-10
localDate7: 2020-05-13
localDate8: 2020-05-12

13. minus(TemporalAmount)

No ADS
Return a copy of this LocalDate object with the amount of time subtracted.
// Inherited from Temporal interface.
public LocalDate minus(TemporalAmount amountToSubtract)
Example:
LocalDate_minus_amount_ex1.java
LocalDate localDate = LocalDate.parse("2020-05-15"); // 2020-May-15
System.out.println("localDate: " + localDate); // 2020-05-15
System.out.println();

// minus(TemporalAmount amountToSubtract)
TemporalAmount amount1 = Period.ofDays(5);
LocalDate localDate1 = localDate.minus(amount1);
System.out.println("localDate1: " + localDate1); // 2020-05-10

// minus(TemporalAmount amountToSubtract)
LocalDate from = LocalDate.of(2020, 5, 10);
LocalDate to = LocalDate.of(2020, 5, 12);

TemporalAmount amount2 = Period.between(from, to); // 2 days
LocalDate localDate2 = localDate.minus(amount2);
System.out.println("localDate2: " + localDate2); // 2020-05-13

14. minus(long, TemporalUnit)

Return a copy of this LocalDate object with a specified value subtracted in given unit.
// Inherited from Temporal interface.
public LocalDate minus(long amountToSubtract, TemporalUnit unit)
Example:
LocalDate_minus_unit_ex1.java
LocalDate localDate = LocalDate.parse("2020-05-15"); // 2020-May-15
System.out.println("localDate: " + localDate); // 2020-05-15
System.out.println();

// minus(long amountToSubtract, TemporalUnit unit)
LocalDate localDate1 = localDate.minus(5, ChronoUnit.DAYS);
System.out.println("localDate1: " + localDate1); // 2020-05-10

// minus(long amountToSubtract, TemporalUnit unit)
LocalDate localDate2 = localDate.minus(10, ChronoUnit.MONTHS);
System.out.println("localDate2: " + localDate2); // 2019-07-15

15. withX(..) *

No ADS
public LocalDate withYear(int year)  

public LocalDate withMonth(int month)  

public LocalDate withDayOfMonth(int dayOfMonth)

public LocalDate withDayOfYear(int dayOfYear)
The withYear(year) method returns a copy of this LocalDate object with the year changed. If the day-of-month is not valid for the year, it will be changed to the last valid day of month.
LocalDate_withYear_ex1.java
LocalDate localDate1 = LocalDate.parse("2020-02-29").withYear(2024);  
System.out.println("localDate1: " + localDate1); // 2024-02-29

LocalDate localDate2 = LocalDate.parse("2020-02-29").withYear(2019); // Important Note!
System.out.println("localDate2: " + localDate2); // 2019-02-28

LocalDate localDate3 = LocalDate.parse("2020-02-28").withYear(2019);  
System.out.println("localDate3: " + localDate3); // 2019-02-28

LocalDate localDate4 = LocalDate.parse("2020-02-21").withYear(2019);  
System.out.println("localDate4: " + localDate4); // 2019-02-21
The withMonth(month) method returns a copy of this LocalDate object with the month changed. If the day-of-month is not valid for the year, it will be changed to the last valid day-of-month.
LocalDate_withMonth_ex1.java
LocalDate localDate1 = LocalDate.parse("2020-03-31").withMonth(2); // Important Note!
System.out.println("localDate1: " + localDate1); // 2020-02-29

LocalDate localDate2 = LocalDate.parse("2020-03-31").withMonth(2); // Important Note!
System.out.println("localDate2: " + localDate2); // 2020-02-29

LocalDate localDate3 = LocalDate.parse("2020-03-29").withMonth(2);  
System.out.println("localDate3: " + localDate3); // 2019-02-28

LocalDate localDate4 = LocalDate.parse("2020-03-21").withMonth(2);  
System.out.println("localDate4: " + localDate4); // 2020-02-21
The withDayOfMonth(dayOfMonth) method returns a copy of this LocalDate object with the day-of-month changed. If the day-of-month is invalid, a DateTimeException is thrown.
LocalDate_withDayOfMonth_ex1.java
LocalDate localDate1 = LocalDate.parse("2020-02-01").withDayOfMonth(15);  
System.out.println("localDate1: " + localDate1); // 2020-02-15

LocalDate localDate2 = LocalDate.parse("2020-02-01").withDayOfMonth(29);  
System.out.println("localDate2: " + localDate2); // 2020-02-29

// Invalid date.
// Throws java.time.DateTimeException: Invalid date 'FEBRUARY 30'
LocalDate localDate3 = LocalDate.parse("2020-02-01").withDayOfMonth(30);  
System.out.println("localDate3: " + localDate3);
Output:
localDate1: 2020-02-15
localDate2: 2020-02-29
Exception in thread "main" java.time.DateTimeException: Invalid date 'FEBRUARY 30'
    at java.base/java.time.LocalDate.create(LocalDate.java:459)
    at java.base/java.time.LocalDate.of(LocalDate.java:271)
    at java.base/java.time.LocalDate.withDayOfMonth(LocalDate.java:1124)
    at org.o7planning.localdate.ex.LocalDate_withDayOfMonth_ex1.main(LocalDate_withDayOfMonth_ex1.java:17)
The withDayOfYear(dayOfYear) method returns a copy of this LocalDate object with the day-of-year changed. If dayOfYear is not valid then a DateTimeException is thrown, valid values from 1 to 365 (Or 366).
LocalDate_withDayOfYear_ex1.java
LocalDate localDate1 = LocalDate.parse("2020-02-01").withDayOfYear(15);  
System.out.println("localDate1: " + localDate1); // 2020-01-15

LocalDate localDate2 = LocalDate.parse("2020-02-01").withDayOfYear(29);  
System.out.println("localDate2: " + localDate2); // 2020-01-29

LocalDate localDate3 = LocalDate.parse("2020-02-01").withDayOfYear(35);  
System.out.println("localDate3: " + localDate3); // 2020-02-04

16. with(TemporalAdjuster)

Return a copy of this LocalDate object, with the date data adjusted by the TemporalAdjuster object.
// Inherited from Temporal interface
public LocalDate with(TemporalAdjuster adjuster)
Example:
LocalDate_with_adjuster_ex1.java
LocalDate now = LocalDate.now();
System.out.println("Now is: " + now);
System.out.println();

LocalDate firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth());
System.out.println("firstDayOfMonth: " + firstDayOfMonth);

LocalDate nextMonday = now.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println("nextMonday: " + nextMonday);

LocalDate firstDayOfNextYear = now.with(TemporalAdjusters.firstDayOfNextYear());
System.out.println("firstDayOfNextYear: " + firstDayOfNextYear);
Output:
Now is: 2021-06-01

firstDayOfMonth: 2021-06-01
nextMonday: 2021-06-07
firstDayOfNextYear: 2022-01-01
Example: Write a custom TemporalAdjuster to find the next Christmas date.
LocalDate_with_adjuster_ex2.java
package org.o7planning.localdate.ex;

import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoField;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;

public class LocalDate_with_adjuster_ex2 {

    public static void main(String[] args) {
        LocalDate localDate = LocalDate.parse("2020-12-28");
        System.out.println("Date : " + localDate);

        // with(TemporalAdjuster):
        LocalDate localDate2 = localDate.with(new NextChristmas());
        System.out.println("Next Christmas : " + localDate2);
    }
}

class NextChristmas implements TemporalAdjuster {

    @Override
    public Temporal adjustInto(Temporal temporal) {
        int month = temporal.get(ChronoField.MONTH_OF_YEAR);
        int day = temporal.get(ChronoField.DAY_OF_MONTH);
        if(month == 12 && day > 25)  {
            temporal = temporal.plus(Period.ofYears(1));
        }
        return temporal.with(ChronoField.MONTH_OF_YEAR, 12).with(ChronoField.DAY_OF_MONTH, 25);
    }
}
Output:
Date : 2020-12-28
Next Christmas : 2021-12-25

17. with(TemporalField, long)

No ADS
Return a copy of this LocalDate object with the specified field changed to the new value.
// Inherited from Temporal interface
public LocalDate with(TemporalField field, long newValue)
In some cases, changing the specified field can cause the resulting date to become invalid, such as changing the month from 31st January to February would make the day-of-month invalid. In cases like this, this problem has been handled by this method. Typically it will choose the previous valid date, which would be the last valid day of February in this example.
Example:
LocalDate_with_field_ex1.java
LocalDate myDate = LocalDate.parse("2021-05-29");

System.out.println("myDate is: " + myDate);
System.out.println("myDate day of week: " + myDate.get(ChronoField.DAY_OF_WEEK));  // 6
System.out.println("myDate day of month: " + myDate.get(ChronoField.DAY_OF_MONTH)); // 29
System.out.println();

LocalDate localDate1 =    myDate.with(ChronoField.DAY_OF_WEEK, 3);   
System.out.println("localDate1: " + localDate1); // 2021-05-26

LocalDate localDate2 =    myDate.with(ChronoField.DAY_OF_MONTH, 10);  
System.out.println("localDate2: " + localDate2);  // 2021-05-10

// February 2021 has only 28 days.
LocalDate localDate3 =    myDate.with(ChronoField.MONTH_OF_YEAR, 2);  
System.out.println("localDate3: " + localDate3);  // 2021-02-28  (***)
Output:
myDate is: 2021-05-29
myDate day of week: 6
myDate day of month: 29

localDate1: 2021-05-26
localDate2: 2021-05-10
localDate3: 2021-02-28
  • ChronoField
  • TemporalField

18. range(TemporalField)

Return the range of valid values for the specified field.
//  Inherited from TemporalAccessor interface
public ValueRange range(TemporalField field)
Example:
LocalDate_range_ex1.java
LocalDate localDate   = LocalDate.parse("2021-06-18");  
System.out.println("LocalDate: "  + localDate);
 
// range(TemporalField field)
ValueRange range = localDate.range(ChronoField.DAY_OF_MONTH);
 
// Print range of DAY_OF_MONTH
System.out.println("Range of DAY_OF_MONTH: "  + range); // 1 - 30

System.out.println("First day of this month: " + range.getMinimum()); // 1
System.out.println("Last day of this month: " + range.getMaximum()); // 30

ValueRange range2 = localDate.range(ChronoField.DAY_OF_YEAR);
System.out.println("Range of DAY_OF_YEAR: "  + range2); // 1 - 365
  • TemporalField
  • ChronoField

19. query(TemporalQuery<R>)

Query this LocalDate object with the given TemporalQuery parameter to extract information.
// Inherited from TemporalAccessor interface
public <R> R query(TemporalQuery<R> query)
Example:
LocalDate_query_ex1.java
package org.o7planning.localdate.ex;

import java.time.LocalDate;
import java.time.temporal.TemporalQueries;

public class LocalDate_query_ex1 {

    public static void main(String[] args) {
        LocalDate localDate = LocalDate.parse("2021-05-15");

        // Apply query method of LocalDate class
        String value = localDate.query(TemporalQueries.precision()).toString();

        // Print the result
        System.out.println("Precision value for LocalDate is " + value);

        // Apply query method of LocalDate class
        System.out.println("Zone value for LocalDate is " + localDate.query(TemporalQueries.offset()));
    }
}
Output:
Precision value for LocalDate is Days
Zone value for LocalDate is null

20. isLeapYear()

Check if the year represented in this LocalDate object is a leap year.
// Inherited from ChronoLocalDate interface
public boolean isLeapYear()
Basically, in the ISO calendar system, a year is considered a leap year if it is divisible by 4. However a year divisible by 100 is not a leap year except when it is divisible by 400.
So:
  • 1904 is a leap year because it was divisible by 4 and not divisible by 100.
  • 1900 is not a leap year because it is divisible by 100 but not by 400.
  • 2000 is a leap year because it is divisible by 400.

21. isSupported(TemporalField)

No ADS
Check if a given TemporalField is supported.
// Inherited from TemporalAccessor interface
public boolean isSupported(TemporalField field)
Basically, LocalDate supports the following TemporalField(s):
  • ChronoField.DAY_OF_WEEK
  • ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH
  • ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR
  • ChronoField.DAY_OF_MONTH
  • ChronoField.DAY_OF_YEAR
  • ChronoField.EPOCH_DAY
  • ChronoField.ALIGNED_WEEK_OF_MONTH
  • ChronoField.ALIGNED_WEEK_OF_YEAR
  • ChronoField.MONTH_OF_YEAR
  • ChronoField.PROLEPTIC_MONTH
  • ChronoField.YEAR_OF_ERA
  • ChronoField.YEAR
  • ChronoField.ERA
Example:
LocalDate_isSupported_ex1.java
LocalDate localDate = LocalDate.parse("2020-05-15"); // 2020-May-15

// boolean isSupported(TemporalField field)
// LocalDate does not support ChronoField.MINUTE_OF_DAY, HOUR_OF_DAY,.. (** Used for LocalDateTime, LocalTime)
System.out.println("MINUTE_OF_DAY is supported? " + localDate.isSupported(ChronoField.MINUTE_OF_DAY)); // false
System.out.println("HOUR_OF_DAY is supported? " + localDate.isSupported(ChronoField.HOUR_OF_DAY)); // false

// LocalDate support ChronoField.MONTH_OF_YEAR, DAY_OF_WEEK,..
System.out.println("MONTH_OF_YEAR is supported? " + localDate.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println("DAY_OF_WEEK is supported? " + localDate.isSupported(ChronoField.DAY_OF_WEEK)); // true
  • TemporalField
  • ChronoField

22. isSupported(TemporalUnit)

Check if a specified TemporalUnit is supported.
// Inherited from Temporal interface
public boolean isSupported(TemporalUnit unit)
Basically, LocalDate supports the following TemporalUnit(s):
  • ChronoUnit.DAYS
  • ChronoUnit.WEEKS
  • ChronoUnit.MONTHS
  • ChronoUnit.YEARS
  • ChronoUnit.DECADES
  • ChronoUnit.CENTURIES
  • ChronoUnit.MILLENNIA
  • ChronoUnit.ERAS
Example:
LocalDate_isSupported_ex2.java
LocalDate localDate = LocalDate.parse("2020-05-15"); // 2020-May-15

// boolean isSupported(TemporalUnit unit)
// LocalDate does not support ChronoUnit.HOURS, MINUTES,.. (** Used for LocalDateTime, LocalTime)
System.out.println("HOURS is supported? " + localDate.isSupported(ChronoUnit.HOURS)); // false
System.out.println("MINUTES is supported? " + localDate.isSupported(ChronoUnit.MINUTES)); // false

// LocalDate support ChronoUnit.CENTURIES, WEEKS,..
System.out.println("CENTURIES is supported? " + localDate.isSupported(ChronoUnit.CENTURIES)); // true
System.out.println("WEEKS is supported? " + localDate.isSupported(ChronoUnit.WEEKS)); // true
No ADS
No ADS