How to Convert Date to LocalDateTime

In this post we will look how to convert Date to LocalDateTime in Java. We will also convert LocalDateTime to Date.

1. Convert Date to LocalDateTime

Java 8 introduces a new API to work with date and time. In this section, let’s see how to convert Date to LocalDateTime with Java. We will also see the option to handle the ZonedDateTime.

1.1 Date To LocalDateTime

Date currentDate = new Date();
 LocalDateTime localDateTime = Instant
     .ofEpochMilli(currentDate.getTime())
     .atZone(ZoneId.systemDefault())
     .toLocalDateTime();

 System.out.println("Locale date time is :" + localDateTime);

 LocalDateTime localDateTime2 = currentDate
     .toInstant()
     .atZone(ZoneId.systemDefault())
     .toLocalDateTime();

 System.out.println("Locale date time is Options 2:" + localDateTime2);

Output

Locale date time is :2020-09-30T16:31:47.655
Locale date time is :2020-09-30T16:31:47.655

1.2 Date To ZonedDateTime

Let’s see how to convert Date to ZonedDateTime in Java

Date currentDate = new Date();
ZonedDateTime zoneDateTime = Instant
     .ofEpochMilli(currentDate.getTime())
     .atZone(ZoneId.systemDefault());

System.out.println("Locale date time is :" + zoneDateTime);

ZonedDateTime zoneDateTime2 = currentDate
     .toInstant()
     .atZone(ZoneId.systemDefault());

System.out.println("Locale date time is Options 2:" + zoneDateTime2);

If you are using a Java 9, there is an addition of an alternative method which simplifies it.

2. Convert LocalDateTime to Date

In this section, let’s see how to convert LocalDateTime to Date in Java.

LocalDateTime now = LocalDateTime.now();
Date date = Date.from(
    now.atZone(ZoneId.systemDefault())
    .toInstant());

System.out.println(date); //Wed Sep 30 17:05:02 PDT 2020

3. Date Utility

It’s better to create an utility class to handle conversion from Date To LocalDateTime and vice versa.

package com.javadevjournal.date;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

public final class JDJDateUtils {

    public static Date convertToDate(LocalDateTime date) {
        return Date.from(date.atZone(ZoneId.systemDefault())
            .toInstant());
    }

    public static LocalDateTime convertToLocalDateTime(Date date) {
        return Instant.ofEpochMilli(date.getTime())
            .atZone(ZoneId.systemDefault())
            .toLocalDateTime();
    }

    public static ZonedDateTime convertToZoneDateTime(Date date) {
        return Instant.ofEpochMilli(date.getTime())
            .atZone(ZoneId.systemDefault());
    }

    public static ZonedDateTime convertToZoneDateTime(Date date, ZoneId zoneId) {
        return Instant.ofEpochMilli(date.getTime())
            .atZone(zoneId);
    }
}

Let’s see how to use this Utility class

Date date = new Date();
LocalDateTime localDateTime = JDJDateUtils.convertToLocalDateTime(date);
System.out.println(localDateTime); // 2020-09-30T17:21:23.676

ZonedDateTime zonedDateTime = JDJDateUtils.convertToZoneDateTime(date);
System.out.println(zonedDateTime); //2020-09-30T17:22:04.055-07:00[America/Los_Angeles]

ZonedDateTime zonedDateTime1 = JDJDateUtils.convertToZoneDateTime(date, ZoneId.of("Asia/Tokyo"));
System.out.println(zonedDateTime1); //2020-10-01T09:22:04.055+09:00[Asia/Tokyo]

LocalDateTime now = LocalDateTime.now();
Date currentDate = JDJDateUtils.convertToDate(now);
System.out.println(currentDate); //Wed Sep 30 17:22:04 PDT 2

4. Date Conversion Details

If  you are interested to understand more about the conversion of date to LocalDateTime in java and vice versa, Let’s discuss few important aspects of the date conversion

  1. The java.util.Date internally represents an instance of timeline and not the actual date (as name suggest).
  2. Internally the Date is stored as long, it’s a count of milliseconds since 1970-01-01T00:00Z (midnight at the start of 1970 GMT/UTC).
  3. Looking at #2, it does not have any information about the time-zone. Try to use the toString() method of the Date and you will see information about the time zone, which is calculated by using the Java’s default time-zone on the fly.
  4. Similarly Instant does not have any information about the time-zone and we need to specify the time zone to convert it to Date (look at the above examples).
  5. We can reply on the system default time zone by using the ZoneId.systemDefault() or passing a specific time zone.

Summary

Date time conversion is a tricky thing but the new Java 8 API provides a flexible and more readable format. In this article, we discussed the following items.

  • How to convert Date to LocalDateTime in Java.
  • Convert Date to ZonedDateTime in Java.
  • How to convert LocalDateTime back to Date.
  • Ability to create a DateUtils to handle date conversion more efficiently.

You can download the source code from our GitHub repository.