What is LocalDate class in Java?
Posted by Nathan on March 5th 2018 15:07 PM
java.time.LocalDate Class in Java : LocalDate is a class in Java which by default returns date in 'yyyy-MM-dd' format. LocalDate class is immutable i.e. contents of this class cannot be changed. Declaration : public final class LocalDate extends Object implements Temporal, ChronoLocalDate, TemporalAdjuster, Serializable Methods of LocalDate Class : LocalDate class has following methods : 1. int compareTo(ChronoLocalDate other): This method is used to compare the dates. 2. LocalDate minusDays(long daysToSubtract) : This method returns copy of this LocalDate by subtracting the provided number of days. 3. LocalDate minusMonths(long monthsToSubtract) : This method returns copy of this LocalDate by subtracting the provided number of months. 4. LocalDateTime atTime(int hour, int minute) : This method is used to combine this date and a time resulting in LocalDateTime. 5. static LocalDate now() : It is used to get the current date from the system clock for the timezone which is set by default. 6. LocalDate plusDays(long daysToAdd) : Add particular number of days to LocalDate. 7. LocalDate plusMonths(long monthsToAdd) : Add particular number of days to LocalDate. 8. boolean equals(Object obj) : This method checks to dates for equality. 9. String format(DateTimeFormatter formatter) : This method is used to format this date with provided formatter. 10. int get(TemporalField field) : This method is used to get the value of the specified field from this date as an int. 11. boolean isLeapYear() : This method is used to check whether the provided year is a leap year or not. Let's see few examples of LocalDate class : Here we will write a code to print today's date, yesterday's date and tomorrow's date. For this code we will use minusDays() and plusDays() methods. In the same code we will check whether present year is leap year or not. Here is the code.