How do I use java.time.LocalDateTime class?

The java.time.LocalDateTime class represents information of both date and time without time-zone. We can create LocalDateTime using the available static factory method such as the of() method or by combining an instance of LocalDate and LocalTime.

The following code snippet will show you both ways. First we begin with using the of() method where we can pass arguments such as the year, month, day, hour, minute and second. On the following line we also use the of() method but this time we pass an instance of LocalDate and LocalTime as the arguments.

package org.kodejava.datetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;

public class LocalDateTimeDemo {
    public static void main(String[] args) {
        // Creates an instance of LocalDateTime using of()
        // static factory method with full date and time arguments.
        LocalDateTime dateTime =
                LocalDateTime.of(2021, Month.SEPTEMBER, 11, 16, 15, 15);
        System.out.println("dateTime  = " + dateTime);

        // Combines LocalDate and LocalTime to creates a new
        // instance of LocalDateTime.
        LocalDate date = LocalDate.now();
        LocalTime time = LocalTime.now();
        LocalDateTime dateTime2 = LocalDateTime.of(date, time);
        System.out.println("dateTime2 = " + dateTime2);

        // Creates LocalDateTime from LocateDate with time using
        // atTime() method.
        LocalDateTime dateTime3 = date.atTime(16, 15, 15);
        LocalDateTime dateTime4 = date.atTime(time);
        System.out.println("dateTime3 = " + dateTime3);
        System.out.println("dateTime4 = " + dateTime4);

        // Creates LocalDateTime from LocalTime with date using
        // atDate() method.
        LocalDateTime dateTime5 = time.atDate(date);
        System.out.println("dateTime5 = " + dateTime5);

        // Obtains LocalDate and LocalTime from LocalDateTime using
        // toLocalDate() and toLocalTime() methods.
        LocalDate date1 = dateTime5.toLocalDate();
        LocalTime time1 = dateTime5.toLocalTime();
        System.out.println("date1 = " + date1);
        System.out.println("time1 = " + time1);
    }
}

We can also create an instance of LocalDateTime by using the LocalDate‘s atTime() method or LocalTime‘s atDate() method as seen in the code snippet above.

On the very end of the code snippet you can see how to obtain a LocalDate or LocalTime information from an instance of LocalDateTime using the toLocalDate() and toLocalTime() method.

Running this code snippet will give you the following result:

dateTime  = 2021-09-11T16:15:15
dateTime2 = 2021-11-16T07:06:36.460459800
dateTime3 = 2021-11-16T16:15:15
dateTime4 = 2021-11-16T07:06:36.460459800
dateTime5 = 2021-11-16T07:06:36.460459800
date1 = 2021-11-16
time1 = 07:06:36.460459800
Wayan

1 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.