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.
Illustration:
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
Class | Description |
---|---|
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 |
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.
Syntax:
public static LocalDate now()
Return value: This method returns the current date using the system clock and default time-zone.
Example 1:
Java
// Java Program to illustrate Commonly used methods in classes // LocalDate, LocalTime and LocalDateTime Classes // Importing input output classes import java.io.*; // Importing LocalDate, LocalTime, LocalDateTime classes // from java.time package import java.time.LocalDate; import java.time.LocalTime; import java.time.LocalDateTime; // Main class class GFG { // Main driver method public static void main (String[] args) { // Creating instance of LocalDate class // using now() method LocalDate presentDate = LocalDate.now(); // Print and display present date System.out.println(presentDate); // Creating instance of LocalDateTime class // using now() method LocalDateTime present = LocalDateTime.now(); System.out.println(present); // Creating instance of LocalTime class // again using now() method LocalTime presentTime = LocalTime.now(); // Print and display the current time System.out.println(presentTime); } } |
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:
Methods | Description |
---|---|
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.
Example 2:
Java
// Java Program to illustrate Commonly used methods in // classes LocalDate, LocalTime and LocalDateTime Classes // Importing input output classes import java.io.*; // Importing java.time package to import classes // LocalDate, LocalTime and LocalDateTime import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating object of LocalDate LocalDate presentDate = LocalDate.now(); System.out.println(presentDate); // Creating object of LocalDateTime LocalDateTime present = LocalDateTime.now(); System.out.println(present); // Creating object of LocalTime LocalTime presentTime = LocalTime.now(); System.out.println(presentTime); // Implementing the LocalDate class methods // All methods of the table are shown below // Print the day of the month System.out.println(presentDate.getDayOfMonth()); // Print the weekday System.out.println(presentDate.getDayOfWeek()); // Print the day w.r.t. the year System.out.println(presentDate.getDayOfYear()); // Print the name of the month System.out.println(presentDate.getMonth()); // Print the name of the month System.out.println(presentDate.getMonthValue()); // Print the boolean value (true/false) System.out.println(presentDate.isLeapYear()); // Print the number of days in that year System.out.println(presentDate.lengthOfYear()); // Print the System.out.println(presentDate.lengthOfMonth()); // Print the new date after adding the number of // days to the current date. System.out.println(presentDate.plusDays( 50 )); // Print the new date after adding the number of // months to the current date. System.out.println(presentDate.plusMonths( 50 )); // Print the new date after adding the number of // years to the current date. System.out.println(presentDate.plusYears( 50 )); // Similarly for the rest of them. System.out.println(presentDate.minusDays( 50 )); System.out.println(presentDate.minusMonths( 50 )); System.out.println(presentDate.minusYears( 50 )); // Implementing methods which are available in // LocalTime and LocalDateTime System.out.println(present.plusHours( 100 )); System.out.println(present.plusMinutes( 1000 )); System.out.println(present.plusSeconds( 100000 )); System.out.println(present.plusNanos( 1000000 )); } } |
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