java.time.LocalDate class, introduced in Java 8, represents a local date without timezone and time of that day e.g. 2019-03-27
. It has the time portion as the moment of the day starts i.e. minutes and seconds are considered zero.
We can use the LocalDate
instances where we need to represent a day without specific time of the day – such as birthday, holiday or leaves taken by an employee.
The primary difference between a java.util.Date
and java.time.LocalDate
is that LocalDate instances are immutable and thread-safe.
1. Class declaration
public final class LocalDate extends Object implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable { //class body }
2. How to create LocalDate
Generally, we will be creating local date instances in two conditions i.e. get current date or create local date for a specified day.
2.1. Get current local date
Use following methods to get the current local date.
LocalDate today = LocalDate.now(); //1 - Recommended LocalDate today = LocalDate.ofInstant(Instant.now(), ZoneId.systemDefault()); //
2.2. Create specified local date
To create a local date with specific day, month and year – use below methods.
LocalDate dateInstance = LocalDate.of(2019, 3, 27); LocalDate dateInstance = LocalDate.parse("2019-03-27");
3. How to parse string to LocalDate
The LocalDate
class has two overloaded parse() methods to convert string date to local date instance.
parse(CharSequence text) //1 parse(CharSequence text, DateTimeFormatter formatter) //2
- Use first method if the string contains date in
ISO_LOCAL_DATE
pattern i.e. yyyy-MM-dd. This is default pattern of local dates in Java. - For any other date pattern, we need to use second method where we pass the date string as well as a formatter which represents the pattern if that date string.
//1 - default date pattern String date = "2019-03-23"; LocalDate localDate = LocalDate.parse(date); //2 - specific date pattern DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-MMM-yyyy"); String date = "23-Mar-2019"; LocalDate localDate = LocalDate.parse(date, formatter);
4. How to format LocalDate to string
Use LocalDate.format(DateTimeFormatter)
method to format a local date to a desired string representation.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-MMM-yyyy"); LocalDate today = LocalDate.now(); String dateString = today.format(formatter); //23-Mar-2019
5. How to modify local dates
LocalDate
provides below methods which can be used to get to new local date instance relative to available local date object.
plusDays()
plusWeeks()
plusMonths()
plusYears()
minusDays()
minusWeeks()
minusMonths()
minusYears()
LocalDate today = LocalDate.now(); //Same date 3 years later LocalDate localDate1 = today.plusYears(3); //local date before 3 months LocalDate localDate2 = today.minusMonths(3);
6. Java LocalDate examples
Let’s get better understanding about LocalDate
class using some examples.
- Parse String to LocalDate in Java
- Format LocalDate to String in Java
- Convert LocalDate to java.util.Date in Java
- Convert Date to LocalDate in Java
- Convert LocalDate to java.sql.Date in Java
- Convert LocalDate to ZonedDateTime in Java
- Convert LocalDate to LocalDateTime in Java
Happy Learning !!
Ref : LocalDate Java Doc