Commonly Used Methods in LocalDate, LocalTime and LocalDateTime Classes in Java
Last Updated :
14 Jul, 2022
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
import java.io.*;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
class GFG {
public static void main (String[] args) {
LocalDate presentDate = LocalDate.now();
System.out.println(presentDate);
LocalDateTime present = LocalDateTime.now();
System.out.println(present);
LocalTime presentTime = LocalTime.now();
System.out.println(presentTime);
}
}
|
Output
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
import java.io.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
class GFG {
public static void main(String[] args)
{
LocalDate presentDate = LocalDate.now();
System.out.println(presentDate);
LocalDateTime present = LocalDateTime.now();
System.out.println(present);
LocalTime presentTime = LocalTime.now();
System.out.println(presentTime);
System.out.println(presentDate.getDayOfMonth());
System.out.println(presentDate.getDayOfWeek());
System.out.println(presentDate.getDayOfYear());
System.out.println(presentDate.getMonth());
System.out.println(presentDate.getMonthValue());
System.out.println(presentDate.isLeapYear());
System.out.println(presentDate.lengthOfYear());
System.out.println(presentDate.lengthOfMonth());
System.out.println(presentDate.plusDays( 50 ));
System.out.println(presentDate.plusMonths( 50 ));
System.out.println(presentDate.plusYears( 50 ));
System.out.println(presentDate.minusDays( 50 ));
System.out.println(presentDate.minusMonths( 50 ));
System.out.println(presentDate.minusYears( 50 ));
System.out.println(present.plusHours( 100 ));
System.out.println(present.plusMinutes( 1000 ));
System.out.println(present.plusSeconds( 100000 ));
System.out.println(present.plusNanos( 1000000 ));
}
}
|
Output
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
Share your thoughts in the comments
Please Login to comment...