Java LocalDate
May 07, 2019
This page will walk through java.time.LocalDate
example. LocalDate
introduced in Java 8, represents a date in the format yyyy-MM-dd such as 2018-12-05. It does not store time or time-zone. LocalDate
is an immutable class and it is the description of the date such as birthdays. LocalDate
is a value-based class and to compare two instances of LocalDate
, we should use its equals
method. We can fetch many other fields of the date from LocalDate
such as day-of-year, day-of-week, month-of-year etc. The format of LocalDate
can be changed using its format
method passing DateTimeFormatter
. The methods of LocalDate
is now, atTime, format, getDayOfMonth, getDayOfWeek, minus, plus, equals, compareTo etc.
For example, get current date.
LocalDate localDate = LocalDate.now();
String formattedDate = localDate.format(DateTimeFormatter.ofPattern("MMM dd, yyyy"));
In Java 9
LocalDate
has been added with more methods such as ofInstant, datesUntil, toEpochSecond. Here on this page we will discuss LocalDate
and its methods with examples.
Contents
- Initializing LocalDate
- LocalDate "plus" Methods
- LocalDate "minus" Methods
- LocalDate "with" Methods
- LocalDate "get" Methods
- LocalDate format()
- LocalDate atTime()
- LocalDate atStartOfDay()
- LocalDate isAfter(), isBefore() and isEqual()
- LocalDate isLeapYear(), isSupported()
- LocalDate lengthOfMonth(), lengthOfYear()
- LocalDate datesUntil()
- LocalDate until()
- LocalDate compareTo() and equals()
- LocalDate adjustInto()
- LocalDate query()
- LocalDate toEpochDay() and toEpochSecond()
- Reference
Initializing LocalDate
LocalDate
gives output in year-month-day format i.e yyyy-MM-dd. We can initialize LocalDate
using following static methods of LocalDate
.
1. now(): Gives
LocalDate
instance with current date from the system clock in the default time-zone.
LocalDate localDate = LocalDate.now(); System.out.println(localDate);
2018-12-05
LocalDate
instance with current date obtaining from specified Clock
.
LocalDate localDate = LocalDate.now(Clock.systemUTC());
Clock.systemUTC()
will return Clock
instance.
3. now(ZoneId zone): Gives
LocalDate
instance with current date from the system clock in the specified time zone.
LocalDate localDate = LocalDate.now(ZoneId.systemDefault());
LocalDate
instance from the given year, month and day of month as int
.
LocalDate localDate = LocalDate.of(2018, 11, 30);
5. of(int year, Month month, int dayOfMonth): Gives
LocalDate
instance from the given year as int
, month as Month
and day of month as int
.
LocalDate localDate = LocalDate.of(2018, Month.NOVEMBER, 30);
6. ofEpochDay(long epochDay): Gives
LocalDate
instance from the given epoch day count.
LocalDate localDate = LocalDate.ofEpochDay(500);
7. ofInstant(Instant instant, ZoneId zone): Gives
LocalDate
instance from the given Instant
and ZoneId
.
LocalDate localDate = LocalDate.ofInstant(Instant.now(), ZoneId.systemDefault());
ofInstant
has been introduced in Java 9.
8. ofYearDay(int year, int dayOfYear): Gives
LocalDate
instance from the given year and day of year as int
.
LocalDate localDate = LocalDate.ofYearDay(2018, 02);
9. parse(CharSequence text): Gives
LocalDate
instance from the given text string such as "2018-10-01".
LocalDate localDate = LocalDate.parse("2018-10-01");
10. parse(CharSequence text, DateTimeFormatter formatter): Gives
LocalDate
instance from the given text string in the given format. The output LocalDate
will be in yyyy-MM-dd format.
LocalDate localDate = LocalDate.parse("15-03-2018", DateTimeFormatter.ofPattern("dd-MM-yyyy"));
11. from(TemporalAccessor temporal): Gives
LocalDate
instance from given temporal object.
LocalDate localDate = LocalDate.from(LocalDate.now());
LocalDate "plus" Methods
Find theLocalDate
methods to add date value by given amount.
1. plus(long amountToAdd, TemporalUnit unit): Returns
LocalDate
instance by adding the given amount.
package com.concretepage; import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class LocalDateDemo { public static void main(String[] args) { LocalDate localDate1 = LocalDate.now(); System.out.println(localDate1); LocalDate localDate2 = localDate1.plus(15, ChronoUnit.DAYS); System.out.println(localDate2); } }
2018-12-02 2018-12-17
2. plus(TemporalAmount amountToAdd): Returns
LocalDate
instance by adding specified TemporalAmount
.
LocalDate localDate1 = LocalDate.now(); System.out.println(localDate1); LocalDate localDate2 = localDate1.plus(Period.ofDays(15)); System.out.println(localDate2);
2018-12-02 2018-12-17
localDate1
and got localDate2
as the instance of LocalDate
.
3. plusDays(long daysToAdd): Returns
LocalDate
instance by adding specified number of days.
LocalDate localDate1 = LocalDate.parse("2018-11-05"); LocalDate localDate2 = localDate1.plusDays(15); System.out.println(localDate2);
4. plusWeeks(long weeksToAdd): Returns
LocalDate
instance by adding specified number of weeks.
LocalDate localDate1 = LocalDate.parse("2018-11-05"); LocalDate localDate2 = localDate1.plusWeeks(5); System.out.println(localDate2);
5. plusMonths(long monthsToAdd): Returns
LocalDate
instance by adding specified number of months.
LocalDate localDate1 = LocalDate.parse("2018-11-05"); LocalDate localDate2 = localDate1.plusMonths(12); System.out.println(localDate2);
6. plusYears(long yearsToAdd): Returns
LocalDate
instance by adding specified number of years.
LocalDate localDate1 = LocalDate.parse("2018-11-05"); LocalDate localDate2 = localDate1.plusYears(2); System.out.println(localDate2);
LocalDate "minus" Methods
Find theLocalDate
methods to subtract date value by given amount.
1. minus(long amountToSubtract, TemporalUnit unit): Returns
LocalDate
instance by subtracting the given amount.
package com.concretepage; import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class LocalDateDemo { public static void main(String[] args) { LocalDate localDate1 = LocalDate.now(); System.out.println(localDate1); LocalDate localDate2 = localDate1.minus(15, ChronoUnit.DAYS); System.out.println(localDate2); } }
2018-12-02 2018-11-17
2. minus(TemporalAmount amountToSubtract): Returns
LocalDate
instance by subtracting specified TemporalAmount
.
LocalDate localDate1 = LocalDate.now(); System.out.println(localDate1); LocalDate localDate2 = localDate1.minus(Period.ofDays(15)); System.out.println(localDate2);
2018-12-02 2018-11-17
localDate1
and got localDate2
as the instance of LocalDate
.
3. minusDays(long daysToSubtract): Returns
LocalDate
instance by subtracting specified number of days.
LocalDate localDate1 = LocalDate.parse("2018-11-05"); LocalDate localDate2 = localDate1.minusDays(15); System.out.println(localDate2);
4. minusWeeks(long weeksToSubtract): Returns
LocalDate
instance by subtracting specified number of weeks.
LocalDate localDate1 = LocalDate.parse("2018-11-05"); LocalDate localDate2 = localDate1.minusWeeks(5); System.out.println(localDate2);
5. minusMonths(long monthsToSubtract): Returns
LocalDate
instance by subtracting specified number of months.
LocalDate localDate1 = LocalDate.parse("2018-11-05"); LocalDate localDate2 = localDate1.minusMonths(12); System.out.println(localDate2);
6. minusYears(long yearsToSubtract): Returns
LocalDate
instance by subtracting specified number of years.
LocalDate localDate1 = LocalDate.parse("2018-11-05"); LocalDate localDate2 = localDate1.minusYears(2); System.out.println(localDate2);
LocalDate "with" Methods
Find theLocalDate
methods that will return LocalDate
adjusting with specified values or setting a new value to a given date field.
1. with(TemporalAdjuster adjuster): Returns
LocalDate
instance adjusting with given TemporalAdjuster
.
LocalDate localDate1 = LocalDate.now(); System.out.println(localDate1); LocalDate localDate2 = localDate1.with(DayOfWeek.SUNDAY); System.out.println(localDate2);
2018-12-03 2018-12-09
with
method. In the current date output, it is Monday. By adjusting with Sunday, we get a new date.
2. with(TemporalField field, long newValue): Returns
LocalDate
instance with specified field to a new value.
LocalDate localDate1 = LocalDate.now(); System.out.println(localDate1); LocalDate localDate2 = localDate1.with(ChronoField.YEAR, 2017); System.out.println(localDate2);
2018-12-03 2017-12-03
3. withDayOfMonth(int dayOfMonth): Returns
LocalDate
instance by changing day of month with given value.
LocalDate localDate1 = LocalDate.now(); System.out.println(localDate1); LocalDate localDate2 = localDate1.withDayOfMonth(10); System.out.println(localDate2);
2018-12-03 2018-12-10
LocalDate
instance by changing day of year with given value. Valid values for day of year is 1 to 365 and for leap year it is 1 to 366.
LocalDate localDate1 = LocalDate.now(); System.out.println(localDate1); LocalDate localDate2 = localDate1.withDayOfYear(110); System.out.println(localDate2);
2018-12-03 2018-04-20
5. withMonth(int month): Returns
LocalDate
instance by changing month of year with given value. Valid values are 1 to 12.
LocalDate localDate1 = LocalDate.now(); System.out.println(localDate1); LocalDate localDate2 = localDate1.withMonth(6); System.out.println(localDate2);
2018-12-03 2018-06-03
LocalDate
instance by changing year with given value.
LocalDate localDate1 = LocalDate.now(); System.out.println(localDate1); LocalDate localDate2 = localDate1.withYear(2017); System.out.println(localDate2);
2018-12-03 2017-12-03
LocalDate "get" Methods
Create aLocalDate
instance as following.
LocalDate localDate = LocalDate.now(); System.out.println(localDate);
2018-12-03
int val = localDate.get(ChronoField.YEAR); System.out.println(val);
2018
IsoChronology val = localDate.getChronology();
int val = localDate.getDayOfMonth(); System.out.println(val);
3
DayOfWeek
.
DayOfWeek val = localDate.getDayOfWeek(); System.out.println(val.name());
MONDAY
int val = localDate.getDayOfYear(); System.out.println(val);
337
IsoEra
.
IsoEra val = localDate.getEra();
long val = localDate.getLong(ChronoField.YEAR); System.out.println(val);
2018
Month
.
Month val = localDate.getMonth(); System.out.println(val.name());
DECEMBER
int val = localDate.getMonthValue(); System.out.println(val);
12
int val = localDate.getYear(); System.out.println(val);
2018
LocalDate format()
The default date format ofLocalDate
is yyyy-MM-dd. The format
method formats the date using specified formatter. Find its declaration.
String format(DateTimeFormatter formatter)
package com.concretepage; import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class LocalDateDemo { public static void main(String[] args) { LocalDate localDate = LocalDate.parse("2018-02-18"); String formattedDate = localDate.format(DateTimeFormatter.ofPattern("MMM dd, yyyy")); System.out.println(formattedDate); } }
Feb 18, 2018
LocalDate atTime()
atTime
method combines LocalDate
with given time and returns LocalDateTime
instance. We can pass following arguments to atTime
method.
atTime(int hour, int minute) atTime(int hour, int minute, int second) atTime(int hour, int minute, int second, int nanoOfSecond) atTime(LocalTime time)
package com.concretepage; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; public class LocalDateDemo { public static void main(String[] args) { LocalDate localDate = LocalDate.parse("2018-05-02"); System.out.println(localDate); //hour and minute LocalDateTime localDateTime = localDate.atTime(16, 50); System.out.println(localDateTime); //hour, minute and second localDateTime = localDate.atTime(16, 50, 20); System.out.println(localDateTime); //hour, minute, second and nanoOfSecond localDateTime = localDate.atTime(16, 50, 20, 300); System.out.println(localDateTime); //Using LocalTime localDateTime = localDate.atTime(LocalTime.now()); System.out.println(localDateTime); } }
2018-05-02 2018-05-02T16:50 2018-05-02T16:50:20 2018-05-02T16:50:20.000000300 2018-05-02T15:26:07.637805900
OffsetTime
to atTime
that will return OffsetDateTime
instance.
OffsetDateTime offsetDateTime = localDate.atTime(OffsetTime.now()); System.out.println(offsetDateTime);
2018-05-02T19:27:14.761376600+05:30
LocalDate atStartOfDay()
atStartOfDay
method combines the LocalDate
with the time of midnight to create LocalDateTime
instance at the start of this date. To get ZonedDateTime
instance, we need to pass ZoneId
instance to atStartOfDay
method. Find the declarations of atStartOfDay
method.
LocalDateTime atStartOfDay() ZonedDateTime atStartOfDay(ZoneId zone)
package com.concretepage; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; public class LocalDateDemo { public static void main(String[] args) { LocalDate localDate = LocalDate.now(); LocalDateTime localDateTime = localDate.atStartOfDay(); System.out.println(localDateTime); ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault()); System.out.println(zonedDateTime); } }
2018-12-03T00:00 2018-12-03T00:00+05:30[Asia/Calcutta]
LocalDate isAfter(), isBefore() and isEqual()
isAfter(ChronoLocalDate other): Checks if this date is after given date.isBefore(ChronoLocalDate other): Checks if this date is before given date.
isEqual(ChronoLocalDate other): Checks if this date is equal to given date.
Find the example.
package com.concretepage; import java.time.LocalDate; public class LocalDateDemo { public static void main(String[] args) { LocalDate localDate1 = LocalDate.parse("2018-03-18"); LocalDate localDate2 = LocalDate.parse("2018-05-12"); System.out.println(localDate1.isAfter(localDate2)); System.out.println(localDate1.isBefore(localDate2)); System.out.println(localDate1.isEqual(localDate2)); } }
false true false
LocalDate isLeapYear(), isSupported()
isLeapYear(): Checks if year is leap year.isSupported(TemporalField field): Checks if given field is supported. Before fetching any field from date we can check if that field is supported otherwise we may get error.
isSupported(TemporalUnit unit): Checks if given unit is supported. Before using plus and minus, we can check if given unit is supported otherwise we may get error
Find the example.
package com.concretepage; import java.time.LocalDate; import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; public class LocalDateDemo { public static void main(String[] args) { LocalDate localDate = LocalDate.parse("2016-03-18"); System.out.println(localDate.isLeapYear()); System.out.println(localDate.isSupported(ChronoField.DAY_OF_MONTH)); System.out.println(localDate.isSupported(ChronoUnit.HOURS)); } }
true true false
LocalDate lengthOfMonth(), lengthOfYear()
lengthOfMonth(): Gives length of month such as 28, 29, 30, 31.lengthOfYear(): Gives length of year either 365 or 366 (leap year).
Find the example.
package com.concretepage; import java.time.LocalDate; public class LocalDateDemo { public static void main(String[] args) { LocalDate localDate = LocalDate.parse("2018-02-18"); System.out.println(localDate.lengthOfMonth()); System.out.println(localDate.lengthOfYear()); } }
28 365
LocalDate datesUntil()
datesUntil
method in LocalDate
has been introduced in Java 9. datesUntil
method returns sequential ordered stream of dates excluding the given date. We can also pass period to set incremental step. We get stream of dates starting from this date until specified date. Specified date will be excluded in the result. datesUntil
is declared as following.
Stream<LocalDate> datesUntil(LocalDate endExclusive) Stream<LocalDate> datesUntil(LocalDate endExclusive, Period step)
package com.concretepage; import java.time.LocalDate; import java.time.Period; import java.util.stream.Stream; public class LocalDateDemo { public static void main(String[] args) { LocalDate localDate = LocalDate.parse("2018-02-18"); System.out.println("--- With LocalDate ---"); Stream<LocalDate> localDateStream = localDate.datesUntil(LocalDate.parse("2018-02-22")); localDateStream.forEach(date -> System.out.println(date)); System.out.println("--- With LocalDate and Period ---"); localDateStream = localDate.datesUntil(LocalDate.parse("2018-02-22"), Period.ofDays(2)); localDateStream.forEach(date -> System.out.println(date)); } }
--- With LocalDate --- 2018-02-18 2018-02-19 2018-02-20 2018-02-21 --- With LocalDate and Period --- 2018-02-18 2018-02-20
LocalDate until()
1. Calculates the period between this date and specified date asPeriod
.
Period until(ChronoLocalDate endDateExclusive)
long until(Temporal endExclusive, TemporalUnit unit)
package com.concretepage; import java.time.LocalDate; import java.time.Period; import java.time.temporal.ChronoUnit; public class LocalDateDemo { public static void main(String[] args) { LocalDate localDate = LocalDate.parse("2018-02-18"); Period period = localDate.until(LocalDate.parse("2018-03-28")); System.out.println(period.getDays()); long val = localDate.until(LocalDate.parse("2018-03-28"), ChronoUnit.DAYS); System.out.println(val); } }
10 38
LocalDate compareTo() and equals()
compareTo(ChronoLocalDate other): Compares this date to specified date.equals(Object obj): Checks if this date is equal to specified date.
Find the example.
LocalDate localDate = LocalDate.parse("2018-02-18"); System.out.println(localDate.equals(LocalDate.parse("2018-02-18"))); System.out.println(localDate.compareTo(LocalDate.parse("2018-02-25")));
true -7
LocalDate adjustInto()
adjustInto
method adjusts the specified temporal object to have the same date as this object. Find the example.
package com.concretepage; import java.time.LocalDate; import java.time.temporal.Temporal; public class LocalDateDemo { public static void main(String[] args) { LocalDate localDate = LocalDate.parse("2018-02-18"); Temporal temporalObj = LocalDate.parse("2017-03-20"); temporalObj = localDate.adjustInto(temporalObj); System.out.println(temporalObj); } }
2018-02-18
temporalObj
has different date value to localDate
. But after adjusting temporalObj
with localDate
, both are same.
LocalDate query()
query
method queries this date using specified query.
TemporalUnit unit = localDate.query(TemporalQueries.precision());
Days
TemporalQueries.precision()
queries for smallest supported unit. It returns the instance of TemporalQuery
.
LocalDate range()
range
gives the range of valid values for the specified field.
LocalDate localDate = LocalDate.parse("2018-02-18"); ValueRange vrange = localDate.range(ChronoField.DAY_OF_MONTH); System.out.println(vrange); vrange = localDate.range(ChronoField.DAY_OF_WEEK); System.out.println(vrange);
1 - 28 1 – 7
LocalDate toEpochDay() and toEpochSecond()
toEpochDay(): Converts this date to epoch day and returns long value.toEpochSecond(LocalTime time, ZoneOffset offset): Converts this date to the number of seconds since the epoch of 1970-01-01T00:00:00Z and returns long value. In has been introduced in Java 9.
Find the example.
LocalDate localDate = LocalDate.parse("2018-02-18"); System.out.println(localDate.toEpochDay()); System.out.println(localDate.toEpochSecond(LocalTime.now(), ZoneOffset.MAX));
17580 1518921065