The java.time.LocalDate
class is part of new date and time API added in Java 8 that represents a date in
the ISO-8601 calendar system, such as 2019-10-03. LocalDate class does not store or represent a time or time-zone.
LocalDate class is immutable thus thread-safe. It is also marked as final so can't be extended.
In this post we’ll see some examples demonstrating usage of Java LocalDate class.
Creating instances of LocalDate
LocalDate class doesn't have any public constructors to obtain an instance, you will use a factory method.
1. Using now()
to obtain an instance of the current date from the system clock in the default time-zone.
LocalDate currentDate = LocalDate.now(); System.out.println(currentDate); //2019-10-26
2. To obtain an instance of LocalDate from a year, month and day using of()
method.
LocalDate date = LocalDate.of(2019, 10, 18); System.out.println(date); // 2019-10-18
Getting Date values from LocalDate
There are methods to get day, month, year value from the LocalDate instance.
public class FormatDate { public static void main(String[] args) { LocalDate date = LocalDate.of(2019, 10, 18); System.out.println("Year-" + date.getYear()); // Using java.time.Month Enum System.out.println("Month-" + date.getMonth()); // Month as int value System.out.println("Month-" + date.getMonthValue()); System.out.println("Day-" + date.getDayOfMonth()); System.out.println("Day of week-" + date.getDayOfWeek()); System.out.println("Day of year-" + date.getDayOfYear()); } }Output
Year-2019 Month-OCTOBER Month-10 Day-18 Day of week-FRIDAY Day of year-291
Check if year is a leap year using LocalDate
Using isLeapYear()
method of LocalDate class in Java you can check if the year is a leap year.
public class FormatDate { public static void main(String[] args) { LocalDate localDate = LocalDate.of(2019, 10, 18); String msg = localDate.isLeapYear()?"is a leap year":"is not a leap year"; System.out.println(localDate.getYear() + " " + msg); localDate = LocalDate.of(2016, 10, 18); msg = localDate.isLeapYear()?"is a leap year":"is not a leap year"; System.out.println(localDate.getYear() + " " + msg); } }Output
2019 is not a leap year 2016 is a leap year
Date calculations using LocalDate
There are methods to add or subtract days, months and years from a LocalDate.
- plus(long amountToAdd, TemporalUnit unit)- Returns a copy of this date with the specified amount added.
- plus(TemporalAmount amountToAdd)- Returns a copy of this date with the specified amount added.
- plusDays(long daysToAdd)- Returns a copy of this LocalDate with the specified number of days added.
- plusMonths(long monthsToAdd)- Returns a copy of this LocalDate with the specified number of months added.
- plusWeeks(long weeksToAdd)- Returns a copy of this LocalDate with the specified number of weeks added.
- plusYears(long yearsToAdd)- Returns a copy of this LocalDate with the specified number of years added.
- minus(long amountToSubtract, TemporalUnit unit)- Returns a copy of this date with the specified amount subtracted.
- minus(TemporalAmount amountToSubtract)- Returns a copy of this date with the specified amount subtracted.
- minusDays(long daysToSubtract)- Returns a copy of this LocalDate with the specified number of days subtracted.
- minusMonths(long monthsToSubtract)- Returns a copy of this LocalDate with the specified number of months subtracted.
- minusWeeks(long weeksToSubtract)- Returns a copy of this LocalDate with the specified number of weeks subtracted.
- minusYears(long yearsToSubtract)- Returns a copy of this LocalDate with the specified number of years subtracted.
LocalDate localDate = LocalDate.of(2019, Month.OCTOBER, 18); System.out.println("Created Local Date - " + localDate);//2019-10-18 System.out.println("Date after subtraction - " + localDate.minusDays(40));//2019-09-08 System.out.println("Date after year subtraction - " + localDate.minusYears(2));//2017-10-18
LocalDate localDate = LocalDate.of(2019, Month.OCTOBER, 18); System.out.println("Created Local Date - " + localDate);//2019-10-18 System.out.println("Date after addition - " + localDate.plusDays(40));//2019-11-27 System.out.println("Date after year addition - " + localDate.plusYears(2));//2021-10-18
Comparing LocalDates in Java
For comparing two LocalDate instances there are the following methods-
- compareTo(ChronoLocalDate other)- Compares this date to another date. Returns the comparator value, negative if less, positive if greater.
- isAfter(ChronoLocalDate other)- Checks if this date is after the specified date. Returns true if this date is after the specified date.
- isBefore(ChronoLocalDate other)- Checks if this date is before the specified date. Returns true if this date is before the specified date.
- isEqual(ChronoLocalDate other)- Checks if this date is equal to the specified date. Returns true if this date is equal to the specified date.
public class FormatDate { public static void main(String[] args) { LocalDate ld1 = LocalDate.of(2019, Month.OCTOBER, 18); LocalDate ld2 = LocalDate.of(2019, Month.SEPTEMBER, 20); System.out.println(ld1.compareTo(ld2)); System.out.println(ld2.compareTo(ld1)); System.out.println(ld1.isAfter(ld2)); System.out.println(ld1.isBefore(ld2)); System.out.println(ld1.isEqual(ld2)); } }Output
1 -1 true false false
Converting String to LocalDate
Check this post for String to LocalDate conversion- Convert String to Date in Java
Converting LocalDate to String
Check this post for LocalDate to String conversion- Convert Date to String in Java
That's all for the topic LocalDate in Java With Examples. If something is missing or you have something to share about the topic please write a comment.You may also like
- LocalTime in Java With Examples
- LocalDateTime in Java With Examples
- ZonedDateTime in Java With Examples
- Method Reference in Java
- Fail-fast And Fail-safe Iterators in Java
- Cannot Make a Static Reference to The Non-static Method or Field
- Arrange Given Numbers to Form The Biggest Number in Java
- Inheritance in Python With Examples
- Spring Boot Properties File: @ConfigurationProperties Example
- React Example - Insert New Object in an Array
No comments:
Post a Comment