Java LocalDate class is introduced in Java 8 in the java.time package. The instance of LocalDate class represents the date without the time zone info. In this guide, we will learn how to use LocalDate class and its methods.
1. LocalDate example to display the current date or any date
Lets take a simple example first to understand how to use LocalDate class to display the current date and a given date.
import java.time.LocalDate; public class Example { public static void main(String[] args) { /* This is how we can display the current date using now() * method */ LocalDate currentDate = LocalDate.now(); System.out.println("Current Date is: "+currentDate); /* We can convert a date to local date instance by using * of() method of the LocalDate */ LocalDate pastDate = LocalDate.of(2016, 01, 23); System.out.println("Given Date is: "+pastDate); } }
Output:
Current Date is: 2017-10-22 Given Date is: 2016-01-23
2. LocalDate example to check whether a given year is leap year or not
By using the isLeapYear()
method of LocalDate class, we can easily check whether the given year is leap year or not. This method returns the boolean value true or false. If the given year is leap then this method returns true else it returns false.
import java.time.LocalDate; public class Example { public static void main(String[] args) { LocalDate givenDate1 = LocalDate.of(2017, 6, 23); System.out.println(givenDate1.isLeapYear()); LocalDate givenDate2 = LocalDate.of(2012, 10, 20); System.out.println(givenDate2.isLeapYear()); LocalDate givenDate3 = LocalDate.of(2000, 11, 14); System.out.println(givenDate3.isLeapYear()); } }
Output:
false true true
3. Convert String to LocalDate
By using the parse() method of the Java LocalDate class we can convert a String to LocalDate.
import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Example { public static void main(String[] args) { //Specify the pattern here that matches the given string DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); //String that contains date as a String String stringDate = "2017/10/23"; //Converting String to LocalDate LocalDate date = LocalDate.parse(stringDate, formatter); //Displaying LocalDate, this will print the date in default format System.out.println("Local Date: "+date); //This will print the date in the format specified in DateTimeFormatter System.out.println("Local Date in the given format: "+formatter.format(date)); } }
Output:
Local Date: 2017-10-23 Local Date in the given format: 2017/10/23
Java – LocalDate methods
1. LocalDateTime atTime(int hour, int minute, int second):
This method combines the date with the specified time to create LocalDateTime.
hour – the hour-of-day to use, from 0 to 23
minute – the minute-of-hour to use, from 0 to 59
second – the second-of-minute to represent, from 0 to 59
2. LocalDateTime atStartOfDay(): This method works same as atTime() method, however in this method we do not specify the time, it combines the time of midnight(start of the day) with the date to create LocalDateTime at the start of the day.
3. int compareTo(ChronoLocalDate other): Compares this date with the specified other date. This method returns negative value if the date is less than other date and returns positive value if the date is greater than the other date.
4. public boolean equals(Object obj): Checks if this date is equal to another date. This method is similar to the compareTo() method except that it returns boolean value true and false based on the comparison.
5. boolean isAfter(ChronoLocalDate other):
Checks if this date is after the specified date. It returns true if this date is after the specified “other date” else it returns false.
6. boolean isBefore(ChronoLocalDate other):
Checks if this date is before the specified date. It returns true if this date is before the specified “other date” else it returns false.
7. boolean isEqual(ChronoLocalDate other):
Checks if this date is equal to the specified “other” date. If both dates are equal then it returns true, else it returns false.
8. static LocalDate now():
This method returns the current date.
9. static LocalDate of(int year, Month month, int dayOfMonth):
Creates the instance of the LocalDate from the given day, month and year
10. static LocalDate ofYearDay(int year, int dayOfYear):
Obtains an instance of LocalDate from the given year and dayOfYear.
11. static LocalDate parse(CharSequence text, DateTimeFormatter formatter):
We have already seen the example of this method above. This method converts the given String to the LocalDate using the format specified by DateTimeFormatter.
12. int getYear():
Returns the year of the date specified by LocalDate.
13. int getMonthValue():
Returns the integer value of the month ranging from 1 to 12.
14. Month getMonth():
Gets the month-of-year field using the Month enum.
15. int getDayOfMonth():
This method returns day of the month. The value ranges from 1 to 31.
16. int getDayOfYear():
from 1 to 365, or 366 in a leap year
17. DayOfWeek getDayOfWeek():
Gets the day-of-week field, which is an enum DayOfWeek.
18. boolean isLeapYear():
We have already seen the example of isLeapYear() method above. This method checks whether the year specified in the LocalDate is leap year or not.
19. int lengthOfYear():
Returns length of the year. The value returned would be 366 if the year is leap, 365 otherwise.
20. LocalDate plusYears(long yearsToAdd):
Returns the LocalDate after adding the specified number of years “yearsToAdd”.
21. LocalDate plusMonths(long monthsToAdd):
Returns the LocalDate after adding the specified number of months “monthsToAdd”.
22. LocalDate plusWeeks(long weeksToAdd):
Returns the LocalDate after adding the specified
number of weeks “weeksToAdd”.
23. LocalDate plusDays(long daysToAdd):
Returns the LocalDate after adding the specified number of days “daysToAdd”.
24. LocalDate minusYears(long yearsToSubtract):
Returns the LocalDate after subtracting the specified number of years “yearsToSubtract”.
25. LocalDate minusMonths(long monthsToSubtract):
Returns the LocalDate after subtracting the specified number of months “monthsToSubtract”.
26. LocalDate minusWeeks(long weeksToSubtract):
Returns the LocalDate after subtracting the specified number of weeks “weeksToSubtract”.
27. LocalDate minusDays(long daysToSubtract):
Returns the LocalDate after subtracting the specified number of days “daysToSubtract”.
28. public Temporal adjustInto(Temporal temporal): The method adjustInto(Temporal temporal) of LocalDate class makes the Temporal object to have the same date as this object
29. static LocalDate ofEpochDay(long epochDay):
Obtains an instance of LocalDate from the specified epoch day count.
Java – LocalDate tutorials
- Java – Convert Date to LocalDate
- Java – Convert LocalDate to Date
- Java – Convert LocalDate to ZonedDateTime
- Java – Convert LocalDate to LocalDateTime
Leave a Reply