Java LocalDate Class
Overview
Java is a simple, object-oriented, robust, platform-independent, concurrent and secure programming language for application development developed by James Gosling. The java.time.LocalDate class in Java is used for representing the current date and time depending on the location.
Introduction to Java LocalDate
LocalDate is an immutable, thread-safe class that represents Date with the default format of yyyy-MM-dd.
- Immutability: all new Date Time APIs are immutable and also good for multithreaded environments.
- Clarity: all functions in same place like now(), format() and parse().
- Utility operations: the new Date Time API comes with methods that perform common tasks.
Declaration
The java.time.LocalDate class is declared as follows:
Fields for Java LocalDate (Table Format With Description for Each)
Following are the fields for class java.time.LocalDate:
- static LocalDate MAX- the LocalDate maximum supported, '+999999999-12-31'.
- static LocalDate MIN- the LocalDate minimum supported, '-999999999-01-01'.
Fields Details
MIN
The minimum supported LocalDate, -999999999-01-01. This can be used by an application as a "far past" date.
MAX
The maximum supported LocalDate is +999999999-12-31. This can be used by an application as a "far future" date.
Methods of Java LocalDate (Table Format With Description for Each)
Important methods of LocalDate class:
Method name | Method description |
---|---|
now() | Obtains the current date from the system clock in the default time zone. |
of(int year, Month month, int dayOfMonth) | Obtains an instance of from a year, month and day. |
of(int year, Month month, int dayOfMonth) | Obtains an instance of from a year, month and day. |
getDayOfMonth() | Gets the day of month field |
getMonth() | Gets the month of year field using the month enum |
getMonthValue() | Gets the month of year field from 1 to 12 |
getYear() | Gets the year field |
isLeapYear() | Checks if the year is a leap year |
plusDays(long)/minusDays(long) | Returns a copy of this with the specified number of days added/subtracted |
plusMonth(long)/minusMonths(long) | Returns a copy of this with the specified number of months added/subtracted |
plusWeek(long)/minusWeeks(long) | Returns a copy of this with the specified number of weeks added/subtracted |
plusYears(long)/minusYears(long) | Returns a copy of this with the specified number of years added/subtracted |
LocalDateTime atTime(int hour, int minute) | It is used for combining date with time. |
int compareTo(ChronoLocalDate other) | It is used for comparing two dates. |
boolean equals(Object obj) | It is used to check if dates are equal or not. |
String format(DateTimeFormatter formatter) | It is used to format the date in a specified formatter |
static LocalDate parse(CharSequence text) | It is used for obtaining the instance of LocalDate from a string, for instance, 2007-12-03 |
static LocalDate parse(CharSequence text, DateTimeFormatter formatter) | It is used for obtaining the instance of LocalDate from a string by using a specified formatter |
Java LocalDate Class Examples
Example 1
Write a Java program to demonstrate LocalDate functions
Output:
Explanation: In this example, the date is displayed in the form of local date format by using the method LocalDate.of().
Example 2
Write a Java program to demonstrate LocalDate functions
Output:
Explanation: In this example, a series of methods are performed on the current date. This is to show you how different methods work.
Example 3
Write a Java program to demonstrate LocalDate functions
Output:
Explanation: In this example, the current date is stored in the variable date. The minusDays(1) operation is performed on the date variable and is stored inside yesterday's variable. This function -1 from the current date. For tomorrow’s date, +1 is addetoin the current date and is printed as tomorrow’s date.
Example 4
Write a Java program to demonstrate LocalDate functions
Output:
Explanation: In this example, the date1.isLeapYear() function is executed with the argument as 2017, since 2017 is not a leap year the output is printed as false. In date2, however, the function returns true since 2016 is a leap year.
Conclusion
- The java.time.LocalDate class in Java is used for representing the current date and time depending on the location.
- LocalDate is an immutable, thread-safe class that represents Date with the default format of yyyy-mm-dd.
- The java.time.LocalDate class is declared as follows: