Commonly Used Methods in LocalDate, LocalTime and LocalDateTime Classes in Java


There are three types of most important classes we can find in a Java environment, related to date and time. LocalDate, LocalTime and LocalDateTime are available in the Java programming to handle the operations related with the date and time problems. Here we need to import the Java package as java.time which is a main application programming interface aka API for date, time, instants, and the time durations.

Purpose of the LocalDate, LocalTime and LocalDateTime classes are −

  • java.time.* //It is the process to include all classes

  • java.time.LocalDate //It is the process for LocalDate

  • java.time.LocalDateTime //It is the process for LocalDateTime

  • java.time.time //It is the process for LocalTime

    • java.time.LocalDate − This function holds a date without any time zone. The format will be as per the ISO - 8601 system, with a default formation.

    • java.time.LocalTime − It is a particular system with a colaboration of ISO 8601 calander system. We will get the local time without any time zone and the date.

    • java.time.LocalDateTime − This particular function gives us the local time and present date without any time zone. It also has a section of nano second while we search for the actual local time.

Here is an example −

localDate: 2022-04-11
localTime: 12:15:26.343
localDateTime: 2022-04-11T12:15:26.344

Algorithm to use the Methods in LocalDate, LocalTime and LocalDateTime Classes

In this possible algorithm, we are going to explain the process how we can use the LocalDate, LocalTime and LocalDateTime classes in a Java environment. By using this algorithm, we will construct some Java syntax which will help us to build some Java codes further.

  • Step 1 − Start the process.

  • Step 2 − Declare a input output stream.

  • Step 3 − Declare and import LocalDate, LocalTime, LocalDateTime classes from the Java time package.

  • Step 4 − Define a main class.

  • Step 5 − Define the argument string.

  • Step 6 − Construct the instance of LocalDate class.

  • Step 7 − Use now() method.

  • Step 8 − Display the present date.

  • Step 9 − Construct the instance of LocalDateTime class.

  • Step 10 − Use now() method.

  • Step 11 − Construct the LocalTime class.

  • Step 12 − Use now() method.

  • Step 13 − Declare the plus hours method.

  • Step 14 − Get the return result.

  • Step 15 − Terminate the process.

Syntax to use the Methods in LocalDate, LocalTime and LocalDateTime Classes

LocalDate localDate = LocalDate.now();
System.out.println("localDate Today: "+localDate);
LocalTime localTime =LocalTime.now();
System.out.println("localTime Today: "+localTime);
LocalDateTime localDateTime =LocalDateTime.now();
System.out.println("localDateTime Today: "+localDateTime);
LocalDate localDate = LocalDate.of(2023,04,11);
System.out.println("localDate Today: "+localDate);
LocalTime localTime =LocalTime.of(23,12,56,234);
System.out.println("localTime Today: "+localTime);
LocalDateTime localDateTime =LocalDateTime.of(2023,12,01,23,12,56,234);
System.out.println("localDateTime Today: "+localDateTime);
LocalDate localDateNov = LocalDate.of(2023, Month. APRIL,11);
System.out.println("localDateApril: "+localDateNov);
System.out.println("Today: "+LocalDate.now());
LocalDate todayPlus10Days=LocalDate.now().plusDays(10);
System.out.println("localDatePlus10: "+todayPlus10Days);
System.out.println("Time Now: "+LocalTime.now());
LocalTime nowMinus20Minutes=LocalTime.now().minusMinutes(20);
System.out.println("nowMinus20Minutes: "+nowMinus20Minutes);
System.out.println("Date-Time Now: "+LocalDateTime.now());
LocalDateTime nowPlus2Years =LocalDateTime.now().plusYears(2);
System.out.println("todayPlus2Years: "+nowPlus2Years);
public static void main(String[] args) {
	LocalDate date = LocalDate.now();
	LocalDate yesterday = date.minusDays(1);
	LocalDate tomorrow = yesterday.plusDays(2);
	System.out.println("Today date: "+date);
	System.out.println("Yesterday date: "+yesterday);
	System.out.println("Tomorrow date: "+tomorrow);
}

In this possible syntax, we have tried to show you how to use the LocalDate, LocalTime and LocalDateTime method classes to get the real value from it. By using these above syntax, we are going to build some Java code to explain the problem statement in an efficient manner.

Approaches to Follow

  • Approach 1 − Java Program to demonstrate the Commonly used methods in classes LocalDate, LocalTime and LocalDateTime Classes

  • Approach 2 − Java Program to illustrate Commonly used methods in classes LocalDate, LocalTime and LocalDateTime Classes by using various methods

  • Approach 3 − Java Program to illustrate Commonly used methods in classes LocalDate, LocalTime and LocalDateTime Classes by using the instant and temporal method

Approach 1: use of LocalDate, LocalTime and LocalDateTime Classes

In this possible approach, we will try to use the now() method of the particular LocalDate class. Here we can get the value of LocalDate based on the system clock with the default time zone.

Example

//Java Program to demonstrate the Commonly used methods in classes LocalDate, LocalTime and LocalDateTime Classes
import java.io.*;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
public class ARBRDD {
   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

2023-04-11
2023-04-11T13:25:11.545488429
13:25:11.545575593

Approach 2: use of LocalDate, LocalTime and LocalDateTime Classes wit GetDay Function

In this particular method, we are going to discuss about some utility methods related to the LocalDate, LocalDateTime, LocalTime classes. They are −

  • getDayOfMonth() − Return the day of the month

  • getDayOfWeek() − Return the weekday

  • getDayOfYear() − Return the day of the year

  • getMonth() − Return the name of the month

  • getMonthValue() − Return the numeric value of the month

Example

//Java Program to illustrate Commonly used methods in classes LocalDate, LocalTime and LocalDateTime Classes by using various methods
import java.io.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class ARBRDD {
   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

2023-04-11
2023-04-11T13:29:55.830090
13:29:55.831305
11
TUESDAY101
APRIL
4
false
365
30
2023-05-31
2027-06-11
2073-04-11
2023-02-20
2019-02-11
1973-04-11
2023-04-15T17:29:55.830090
2023-04-12T06:09:55.830090
2023-04-12T17:16:35.830090
2023-04-11T13:29:55.831090

Approach 3: get Time, Date and Datetime With Instant and, Temporal Method

Use of the Instant Method

In this possible Java example, we have tried to show you how to implement an instant class as well as a temporal method to get the local date and time without mentioning the user’s current time zone.

Example

//Java Program to illustrate Commonly used methods in classes LocalDate, LocalTime and LocalDateTime Classes by using the instant method
package com.howtodoinjava.core.datetime;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
public class AddHoursMinutesSecondsToDate {
   public static void main(final String[] args) {
      //1 - Same methodss for ZonedDateTime and OffsetDateTime
      LocalDateTime now = LocalDateTime.now();
      System.out.println("Current Date and Time = " + now);
      LocalDateTime updatedTime = now.plusHours(2);
      System.out.println("Updated Date and Time = " + updatedTime);
      updatedTime = now.plusMinutes(20);
      System.out.println("Updated Date and Time = " + updatedTime);
      updatedTime = now.plus(Duration.ofMillis(8000));
      System.out.println("Updated Date and Time = " + updatedTime);
      updatedTime = now.plus(20, ChronoUnit.HOURS);
      System.out.println("Updated Date and Time = " + updatedTime);
       updatedTime = now.minusHours(2);
      updatedTime = now.minusMinutes(20);
      updatedTime = now.minusSeconds(300);
      updatedTime = now.minus(Duration.ofMillis(8000));
      updatedTime = now.minus(20, ChronoUnit.HOURS);
      Instant currentInstant = Instant.parse("2022-06-24T05:12:35Z");
      System.out.println("Current Date and Time = " + currentInstant);
      Instant updatedInstant = currentInstant.plus(2, ChronoUnit.HOURS);
      System.out.println("Updated Date and Time = " + updatedInstant);
      updatedInstant = currentInstant.plusSeconds(300);
      updatedInstant = currentInstant.plusMillis(8000);
      updatedInstant = currentInstant.plusNanos(600000);
      updatedInstant = currentInstant.plusSeconds(300);
      System.out.println("Updated Date and Time = " + updatedInstant);
      //2 java.util.Date
      Date date = new Date();
      Calendar cal = Calendar.getInstance();
      cal.setTime(date);
      System.out.println("Current Date and Time = " + cal.getTime());
      cal.add(Calendar.HOUR, 2);
      System.out.println("Updated Date and Time = " + cal.getTime());
      cal.add(Calendar.MINUTE, -15);
      System.out.println("Updated Date and Time = " + cal.getTime());
      cal.add(Calendar.SECOND, 10);
      System.out.println("Updated Date and Time = " + cal.getTime());
   }
}

Output

Current Date and Time = 2023-04-11T13:32:32.989642529
Updated Date and Time = 2023-04-11T15:32:32.989642529
Updated Date and Time = 2023-04-11T13:52:32.989642529
Updated Date and Time = 2023-04-11T13:32:40.989642529
Updated Date and Time = 2023-04-12T09:32:32.989642529
Current Date and Time = 2022-06-24T05:12:35Z
Updated Date and Time = 2022-06-24T07:12:35Z
Updated Date and Time = 2022-06-24T05:17:35Z
Current Date and Time = Tue Apr 11 13:32:32 GMT 2023
Updated Date and Time = Tue Apr 11 15:32:32 GMT 2023
Updated Date and Time = Tue Apr 11 15:17:32 GMT 2023
Updated Date and Time = Tue Apr 11 15:17:42 GMT 2023

Use of the Instant Method

In this possible Java example, we have tried to show you how to implement the temporal method to get the value of the local date and time without mentioning the user’s current time zone.

Example

//Java Program to illustrate Commonly used methods in classes LocalDate,
LocalTime and LocalDateTime Classes by using the temporal method
package com.howtodoinjava.core.datetime;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DaysBetweenDates {
   public static void main(final String[] args) {
      LocalDate date1 = LocalDate.now();
      LocalDate date2 = date1.plusDays(99);
      long diffInDays = ChronoUnit.DAYS.between(date1, date2);
      System.out.println(diffInDays);
      diffInDays = date1.until(date2, ChronoUnit.DAYS);
      System.out.println(diffInDays);
      LocalDate startDate = LocalDate.now();
      LocalDate endDate = startDate.plusMonths(2);
      long numOfDays = ChronoUnit.DAYS.between(startDate, endDate);
      List<LocalDate> listOfDates = Stream.iterate(startDate, date ->
      date.plusDays(1))
      .limit(numOfDays)
      .collect(Collectors.toList());
      System.out.println(listOfDates);
   }
}

Output

99
99
[2023-04-11, 2023-04-12, 2023-04-13, 2023-04-14, 2023-04-15, 2023-04-16, 2023-
04-17, 2023-04-18, 2023-04-19, 2023-04-20, 2023-04-21, 2023-04-22, 2023-04-23,
2023-04-24, 2023-04-25, 2023-04-26, 2023-04-27, 2023-04-28, 2023-04-29, 2023-
04-30, 2023-05-01, 2023-05-02, 2023-05-03, 2023-05-04, 2023-05-05, 2023-05-06,
2023-05-07, 2023-05-08, 2023-05-09, 2023-05-10, 2023-05-11, 2023-05-12, 2023-
05-13, 2023-05-14, 2023-05-15, 2023-05-16, 2023-05-17, 2023-05-18, 2023-05-19,
2023-05-20, 2023-05-21, 2023-05-22, 2023-05-23, 2023-05-24, 2023-05-25, 2023-
05-26, 2023-05-27, 2023-05-28, 2023-05-29, 2023-05-30, 2023-05-31, 2023-06-01,
31
2023-06-02, 2023-06-03, 2023-06-04, 2023-06-05, 2023-06-06, 2023-06-07, 2023-
06-08, 2023-06-09, 2023-06-10]

Conclusion

java.time classes are the immutable classes those represents the value of date and time value with a default format. In this article todat, we have tried to build some Java syntax with an algoritm, and after that demontrated some possible Java codes to explain the problem statement in an efficient manner.

Updated on: 27-Dec-2023

29 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements