Java 8 LocalDate
In this article, we will take a look at LocalDate
in java, its meaning and how to create a date with year, month and date in java.
LocalDate
is a new class added in java 8 in java.time
package. It represents a date without any timezone information.
If you want to store just a date, then this class should be used in preference to java.util.Date
.
LocalDate
is an immutable class. This means that once an instance of LocalDate
is created, it cannot be modified.
LocalDate
is also a final class. This means that you cannot extend and modify its functionality.
Creating LocalDate
A LocalDate
object can be created using its static now()
method as shown below
LocalDate current = LocalDate.now(); System.out.println(current); // prints 2021-09-07
now()
returns the current date in yyyy-MM-dd
format.
Create LocalDate with year, month & date
LocalDate
provides of()
method which takes three integer arguments representing year, month and day respectively. It returns a LocalDate
with the values supplied. Example,
LocalDate date = LocalDate.of(2021, 9, 7); System.out.println(date); // prints 2021-09-07
Values for year, month and date must be valid.
Thus, LocalDate.of(2021, 15, 35)
will throw
java.time.DateTimeException: Invalid value for MonthOfYear (valid values 1 – 12): 15
Create LocalDate from string
If you have a date in string form, it can be converted to a LocalDate
using its parse()
method as shown below
LocalDate date = LocalDate.parse("2021-09-07"); System.out.println(date); // prints 2021-09-07
There are two restrictions with parse()
:
1. Format of date string must be yyyy-MM-dd
.
2. Year must be of 4 digits, that is, 21 is invalid. Month and date must be of 2 digits, that is, 9 is invalid; it must be 09.
If you want to parse dates of different format, then use overloaded parse()
method, which takes an object of DateTimeFormatter
with custom format.
Below code is used to parse a date in dd-MM-yyyy
format
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy"); LocalDate date = LocalDate.parse("09-07-2021",format); System.out.println(date); // prints 2021-09-07
Get Fields from LocalDate
If you have a LocalDate
object, then you can get fields of this date such as year, month as a number, name of month, date, day of month, day of year etc., as shown below
LocalDate now = LocalDate.now(); System.out.println("Date:" + now); System.out.println("Year: " + now.getYear()); System.out.println("Month name: " + now.getMonth()); System.out.println("Month value: " + now.getMonthValue()); System.out.println("Day of month: " + now.getDayOfMonth()); System.out.println("Day of year: " + now.getDayOfYear()); System.out.println("Day of week: " + now.getDayOfWeek());
This prints
Date:2021-09-08
Year: 2021
Month name: SEPTEMBER
Month value: 9
Day of month: 8
Day of year: 251
Day of week: WEDNESDAY
Check Leap Year with LocalDate
LocalDate
has an isLeapYear()
, which checks its year to be a leap year and returns true
or false
accordingly. Example,
LocalDate now = LocalDate.now(); System.out.println(now.isLeapYear()); // false
Comparing LocalDates
LocalDate
contains methods that can be used to check if the dates represented by two LocalDate
objects fall before/after each other or they are same. Example,
LocalDate now = LocalDate.now(); // create a date for next year LocalDate nextYear = LocalDate.of(now.getYear() + 1, now.getMonthValue(), now.getDayOfMonth()); System.out.println(nextYear.isAfter(now)); // true System.out.println(now.isBefore(nextYear)); // false System.out.println(now.isEqual(nextYear)); // false System.out.println(now.compareTo(nextYear)); // -1
All methods are self-explanatory except compareTo()
, which returns an integer value:
-1 : if the LocalDate
on which it is called, is before the argument object.
0 : if the LocalDate
on which it is called, represents the same date as the argument object.
1 : if the LocalDate
on which it is called, is after the argument object.
Creating LocalDate from another
In the last example, we created a LocalDate
for next year by getting year, month and date from another LocalDate
object and then used of() method with these values.
LocalDate
provides methods to create a new LocalDate
from an existing one by altering one of its fields. These methods are
withYear()
It takes an integer and returns a new LocalDate
object with year set to the supplied value.
withMonth()
It takes an integer and returns a new LocalDate
object with month set to the supplied value.
withDayOfMonth()
It takes an integer and returns a new LocalDate
object with date set to the supplied value.
All of these methods return a new LocalDate
object. The object on which these methods are invoked remains unaltered. Example,
LocalDate now = LocalDate.now(); LocalDate nextYear = now.withYear(now.getYear()+1); LocalDate tomorrow = now.withDayOfMonth(now.getDayOfMonth()+1); LocalDate lastMonth = now.withMonth(12); System.out.println(nextYear); // 2022-09-08 System.out.println(tomorrow); // 2021-09-09 System.out.println(lastMonth); // 2021-12-08
That is all on LocalDate
in java 8.
We saw how to create LocalDate
set to current date with now()
and with year, month and date using of()
method and parsing a string date using parse()
method.
We also looked at comparing two LocalDate
objects using isBefore()
, isAfter()
, and isEqual()
and compareTo()
methods.
Finally, we saw how to create a LocalDate
object from another object by modifying one of its fields.