Java LocalDate class is part of Java 8 Date API.

Java LocalDate

  • Java LocalDate is immutable class and hence thread safe.
  • LocalDate provides date output in the format of YYYY-MM-dd.
  • java-localdate
  • The LocalDate class has no time or Timezone data. So LocalDate is suitable to represent dates such as Birthday, National Holiday etc.
  • LocalDate class implements Temporal, TemporalAdjuster, ChronoLocalDate and Serializable interfaces.
  • LocalDate is a final class, so we can’t extend it.
  • LocalDate is a value based class, so we should use equals() method for comparing if two LocalDate instances are equal or not.

Importing Java LocalDate Class

Java LocalDate class is in java.time package. So you can import it using import statement:

Creating LocalDate instance

There are four standard ways to create LocalDate instance.

  1. Invoking the static now() method that returns the current date from system clock.
  2. By passing Year, Month, and Day values to LocalDate of() method
  3. Using LocalDate parse() method.
  4. Using LocalDate ofInstant() method as shown below.

Retrieving Date Information from LocalDate Class

Java LocalDate class provides a lot of useful methods to get the more details about date, for example year, month, day of year etc. Let’s look into these through some example programs.

  1. getYear(): returns the year contained within the LocalDate object.
  2. getMonth(): returns the month contained within the LocalDate object.
  3. getDayOfMonth(): returns the day of the month contained within the LocalDate object.
  4. getDayOfWeek(): returns the day of the week from the LocalDate object.
  5. getDayOfYear(): returns the day of the year from the LocalDate instance.

Java LocalDate methods – plus and minus

  1. plusYears(long yearsToAdd): adds the yearsToAdd to the year value and return the copy of LocalDate object. Since LocalDate is immutable, the specified object doesn’t change. Also if the new date is invalid then the last valid date is returned.
  2. plusWeeks(long weeksToAdd): returns a new LocalDate instance after adding weeksToAdd weeks, no change in the specified object.
  3. plusMonths(long monthsToAdd): returns a copy of specified LocalDate object after adding monthsToAdd months.
  4. plusDays(long daysToAdd): returns a copy of specified LocalDate object after adding daysToAdd days.
  5. minusDays(long daysToSubtract): returns a copy of specified LocalDate object after subtracting daysToSubtract days.
  6. minusWeeks(long weeksToSubtract): returns a copy of specified LocalDate object after subtracting weeksToSubtract weeks.
  7. minusMonths(long monthsToSubtract): returns a copy of specified LocalDate object after subtracting monthsToSubtract months.
  8. minusYears(long yearsToSubtract): returns a copy of specified LocalDate object after subtracting yearsToSubtract years. If the new LocalDate is invalid (29-Feb), then last valid date is returned.

Java LocalDate to Date

We should avoid using legacy java.util.Date class but sometimes we have to convert LocalDate to Date for legacy support. Below are two ways to convert LocalDate to Date, you can use any of them.

That’s all for java LocalDate class.

References: API Doc, StackOverflow Article

By admin

Leave a Reply