Java LocalDate
- Java LocalDate is immutable class and hence thread safe.
- LocalDate provides date output in the format of YYYY-MM-dd.
- 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
andSerializable
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:
1 2 3 |
import java.time.LocalDate; |
Creating LocalDate instance
There are four standard ways to create LocalDate instance.
- Invoking the static
now()
method that returns the current date from system clock.
1234LocalDate ldObj = LocalDate.now();System.out.println(ldObj); //prints "2017-11-10" - By passing Year, Month, and Day values to LocalDate
of()
method
1234LocalDate ldObj = LocalDate.of(2017, 11, 6);System.out.println(ldObj); //prints "2017-11-06" - Using LocalDate
parse()
method.
123LocalDate ld = LocalDate.parse("2017-11-10"); - Using LocalDate
ofInstant()
method as shown below.
123LocalDate ld = LocalDate.ofInstant(Instant.now(), ZoneId.systemDefault());
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.
getYear()
: returns the year contained within the LocalDate object.
12345LocalDate ld = LocalDate.of(2017, 11, 10);int year = ld.getYear();System.out.println(year); //prints "2017"getMonth()
: returns the month contained within the LocalDate object.
12345LocalDate ld = LocalDate.now(); // today is 10-Nov-2017Month month = ld.getMonth();System.out.println(month + "," + month.getValue()); // prints "NOVEMBER,11"getDayOfMonth()
: returns the day of the month contained within the LocalDate object.
12345LocalDate ld = LocalDate.now(); // today is 10-Nov-2017int dayOfMonth = ld.getDayOfMonth();System.out.println(dayOfMonth); // prints "10"getDayOfWeek()
: returns the day of the week from the LocalDate object.
12345LocalDate ld = LocalDate.now(); // today is 10-Nov-2017DayOfWeek dayOfWeek = ld.getDayOfWeek();System.out.println(dayOfWeek + "," + dayOfWeek.getValue()); // prints "FRIDAY,5"getDayOfYear()
: returns the day of the year from the LocalDate instance.
12345LocalDate ld = LocalDate.of(2017, 2, 10);int dayOfYear = ld.getDayOfYear();System.out.println(dayOfYear); // prints "41" i.e. 31+10
Java LocalDate methods – plus and minus
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.
12345678LocalDate ld = LocalDate.of(2017, 2, 10);LocalDate ldNew = ld.plusYears(3);System.out.println(ldNew + ":" + ld); // prints "2020-02-10:2017-02-10"ld = LocalDate.of(2016, 2, 29);ldNew = ld.plusYears(1); // 2017-02-29 is invalid date, so it will return 2017-02-28System.out.println(ldNew); // prints "2017-02-28"plusWeeks(long weeksToAdd)
: returns a new LocalDate instance after adding weeksToAdd weeks, no change in the specified object.
12345LocalDate ld = LocalDate.of(2017, 11, 10);LocalDate ldNew = ld.plusWeeks(3);System.out.println(ldNew); // 2017-12-01plusMonths(long monthsToAdd)
: returns a copy of specified LocalDate object after adding monthsToAdd months.
12345LocalDate ld = LocalDate.of(2017, 11, 10);LocalDate ldNew = ld.plusMonths(3);System.out.println(ldNew); // 2018-02-10plusDays(long daysToAdd)
: returns a copy of specified LocalDate object after adding daysToAdd days.
12345LocalDate ld = LocalDate.of(2017, 11, 10);LocalDate ldNew = ld.plusDays(10);System.out.println(ldNew); // 2017-11-20minusDays(long daysToSubtract)
: returns a copy of specified LocalDate object after subtracting daysToSubtract days.
12345LocalDate ld = LocalDate.of(2017, 11, 10);LocalDate ldNew = ld.minusDays(10);System.out.println(ldNew); // 2017-10-31minusWeeks(long weeksToSubtract)
: returns a copy of specified LocalDate object after subtracting weeksToSubtract weeks.
12345LocalDate ld = LocalDate.of(2017, 11, 10);LocalDate ldNew = ld.minusWeeks(1);System.out.println(ldNew); // 2017-11-03minusMonths(long monthsToSubtract)
: returns a copy of specified LocalDate object after subtracting monthsToSubtract months.
12345LocalDate ld = LocalDate.of(2017, 11, 10);LocalDate ldNew = ld.minusMonths(1);System.out.println(ldNew); // 2017-10-10minusYears(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.
12345LocalDate ld = LocalDate.of(2016, 2, 29);LocalDate ldNew = ld.minusYears(2);System.out.println(ldNew); // 2014-02-28
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.journaldev.java; import java.time.LocalDate; import java.time.ZoneId; import java.util.Date; public class JavaLocalDateExample { public static void main(String args[]) { LocalDate ld = LocalDate.now(); Date date = Date.from(ld.atStartOfDay(ZoneId.systemDefault()).toInstant()); System.out.println(date); // prints "Fri Nov 10 00:00:00 IST 2017" Date date1 = java.sql.Date.valueOf(ld); System.out.println(date1); // prints "2017-11-10" } } <img class="alignnone wp-image-30593 size-full" src="https://all-learning.com/wp-content/uploads/2017/11/java-localdate-to-dates.png" alt="java-localdate-to-date" width="1208" height="856" /> |
That’s all for java LocalDate class.
References: API Doc, StackOverflow Article