LocalDate class is provided by java.time package in Java 8. This class respresents a date often in the form of year-month-date, without a time-zone in the ISO-8601 calendar system.
This class does not store or represent time and is used to represent date like your birthday or anniversary, for example 2021-03-13.
This class is immutable and thread-safe.
Following are three common ways to get an instance of LocalDate:
LocalDate representing current date
LocalDate localDate = LocalDate.now();
LocalDate instance from Year, Month and Date
LocalDate localDate = LocalDate.of(year, month, day);
ex: LocalDate localDate = LocalDate.of(2021, 3, 13);
LocalDate instance from a String
LocalDate localDate = LocalDate.parse("2021-03-13");
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
System.out.println("Current date : " + localDate);
LocalDate localDate2 = LocalDate.of(2021, 3, 13);
System.out.println("Current Date from year, month and date : " + localDate2);
LocalDate localDate3 = LocalDate.parse("2021-03-13");
System.out.println("Current date from a string: " + localDate3);
}
}
Output
Current date : 2021-03-13
Current Date from year, month and date : 2021-03-13
Current date from a string: 2021-03-13
Get information about a particular date
LocalDate class provides many methods to get information about a particular date. Following are the commonly used methods:
| int getDayOfMonth() | Returns the day of month |
| DayOfWeek getDayOfWeek() | Returns the day of week |
| int getDayOfYear() | Returns the day of year |
| Month getMonth() | Returns the month-of-year |
| int getMonthValue() | Returns the month-of-year value from 1 to 12 |
| int getYear() | Returns the year field |
| int lengthOfMonth() | Returns the length of month |
| int lengthOfYear() | Returns the length of year |
| String toString() | Returns the String representation of the date. |
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
System.out.println("String representation of date : " + localDate.toString());
System.out.println("Day of Month : " + localDate.getDayOfMonth());
System.out.println("Day of week : " + localDate.getDayOfWeek());
System.out.println("Day of year : " + localDate.getDayOfYear());
System.out.println("Month : " + localDate.getMonth());
System.out.println("Month value : " + localDate.getMonthValue());
System.out.println("Year : " + localDate.getYear());
System.out.println("Length of month : " + localDate.lengthOfMonth());
System.out.println("Length of year : " + localDate.lengthOfYear());
}
}
Output
String representation of date : 2021-03-13
Day of Month : 13
Day of week : SATURDAY
Day of year : 72
Month : MARCH
Month value : 3
Year : 2021
Length of month : 31
Length of year : 365
Date manipulation
We usually have the requirement to add days, weeks, months or years to a date. LocalDate provides methods for date manipulation. Following are the commonly used methods provided by the LocalDate class:
| LocalDate plusDays(long daysToAdd) | To add specified number of days |
| LocalDate plusMonths(long monthsToAdd) | To add specified number of months |
| LocalDate plusWeeks(long weeksToAdd) | To add specified number of weeks |
| LocalDate plusYears(long yearsToAdd) | To add specified number of years |
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
System.out.println("Current Date : " + localDate.toString());
System.out.println("Date after adding 30 Days : " + localDate.plusDays(30));
System.out.println("Date after adding 2 months : " + localDate.plusMonths(2));
System.out.println("Date after adding 4 weeks : " + localDate.plusWeeks(4));
System.out.println("Date after adding 2 years : " + localDate.plusYears(2));
}
}
Output
Current Date : 2021-03-13
Date after adding 30 Days : 2021-04-12
Date after adding 2 months : 2021-05-13
Date after adding 4 weeks : 2021-04-10
Date after adding 2 years : 2023-03-13
Date comparison
LocalDate class provides methods for date comparison. LocalDate is a value based classed so equals method should be used for comparison. Following are commonly used methods for comparison:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println("Today : " + today.toString());
LocalDate tomorrow = LocalDate.now().plusDays(1);
System.out.println("Tomorrow : " + tomorrow);
System.out.println("Does tomorrow comes after today : " + tomorrow.isAfter(today));
System.out.println("Does tomorrow comes before today : " + tomorrow.isBefore(today));
System.out.println("Is tomorrow equals to today : " + tomorrow.isEqual(today));
}
}
Output
Today : 2021-03-13
Tomorrow : 2021-03-14
Does tomorrow comes after today : true
Does tomorrow comes before today : false
Is tomorrow same as today : false
