Commonly Used Methods in LocalDate, LocalTime and LocalDateTime Classes in Java
Java provides the three most important classes related to dates namely LocalDate, LocalTime, LocalDateTime which makes the handling of dates in Java very easy as in order to use these classes, we need to import ‘java.time’ package which is main the main API for dates, times, instants, and durations.
1. java.time.* // To include all classes 2. java.time.LocalDate // for LocalDate 3. java.time.LocalDateTime // for LocalDateTime 4. java.time.time // for LocalTime
|LocalDate||LocalDate class can hold the Date only. For example, say be it 2021-02-28|
|LocalTime||LocalTime class can hold the Time only. For example, say be it 19:32:25.457826|
LocalDateTime class holds both the Date and Time. For example, say be it 2021-02-28T19:32:25.457826.
In this format, Before T is the date, and after T is the Time.
It is generally said considered bad practice to import all the unnecessary files of the package. Therefore, we should import only the classes which are needed. There are various methods present in these classes to handle date and time according to the class. Lets us discuss the most frequent method of LocalDate class known as now() method.
Method: now() method
The now() method of a LocalDate class used to obtain the current date from the system clock in the default time-zone. This method will return LocalDate based on the system clock with the default time-zone to obtain the current date.
public static LocalDate now()
Return value: This method returns the current date using the system clock and default time-zone.
2021-02-28 2021-02-28T14:16:07.181034 14:16:07.181230
We will be discussing the rest of the utility methods in the table shown below in a single code in the implementation part which is discussed further. All the classes have been discussed namely the LocalDate, LocalDateTime, LocalTime classes are immutable and all the modification method returns new object and therefore do not change the value of the current object.
Some utility methods provided by the LocalDate class are as follows:
|getDayOfMonth()||Returns the day of the month. For example, say it be 28|
|getDayOfWeek()||Returns the weekday. For example, say it be SUNDAY|
|getDayOfYear()||Returns the day w.r.t. the year. For example, say it be 59|
|getMonth()||Returns the name of the month. For example, say it be FEBRUARY|
|getMonthValue()||Returns the numeric value of the month. For example, say it be 2|
|isLeapYear()||Returns boolean value (true/false). For example, say it be false.|
|lengthOfYear()||Returns the number of days in that year. For example, say it be 365.|
|lengthOfMonth()||Returns the number of days in that year. For example, say ut be 28.|
|plusDays(numberOfDaysToBeAdded)||Returns a new date after adding the number of days to the current date.|
|plusMonths(numberOfMonthsToBeAdded)||Returns a new date after adding the number of months to the current date.|
|plusYears(numberOfYearsToBeAdded)||Returns a new date after adding the number of years to the current date.|
Similar methods are available of subtracting date. minusDays(), minusMonths(), minusYears(). These functions are also available for the LocalDateTime class, adding some more related to time. Such as plusHours(), plusMinutes(), plusSeconds(), plusNanos() and many more.
2021-02-28 2021-02-28T14:54:21.331923 14:54:21.332156 28 SUNDAY 59 FEBRUARY 2 false 365 28 2021-04-19 2025-04-28 2071-02-28 2021-01-09 2016-12-28 1971-02-28 2021-03-04T18:54:21.331923 2021-03-01T07:34:21.331923 2021-03-01T18:41:01.331923 2021-02-28T14:54:21.332923