Saturday, July 23, 2022

How to convert Date to LocalDate and LocalDateTime in Java 8 - Example Tutorial

The easiest way to convert a java.util.Date to java.time.LocalDate is via Instant, which is the equivalent class of java.util.Date in JDK 8. You can first convert util Date to Instant and then create a LocalDateTime object from that instant at your default timezone. Once you have an instant of LocalDateTime, you can easily convert that to LocalDate or LocalTime in Java. If you are not living under the rock in the last two years that its 2016 and Java 8 has been around for more than 2 years, 18th March 2014 Java SE 8 GA happened.

In the third attempt of designing a robust date and time API, JDK 8 has come up with JSR 318 new Date and Time API, and many programmers have already started using it.

The new API is meant to replace our old but not so gold, but still ubiquitous java.util.Date and Calendar API, but unfortunately conversion between java.util.Date to java.time.LocalDate, java.time.LocalTime is not that straightforward.

You need to cross a couple of bridges to reach there. This is the start of the Java 8 conversion tutorial series where I'll share lots of tutorials about converting old date classes to the new date and time classes in Java 8.

Btw, if you are not familiar with the new Date and Time API added on Java 8 then I suggest you first go through a comprehensive and up-to-date Java course like The Complete Java MasterClass on Udemy. It's also very affordable and you can buy in just $10 on Udemy sales which happen every now and then.





Steps to convert java.util.Date to java.time.LocalDate in Java

Here are the steps you need to follow to perform this conversion:
  1. Convert java.util.Date to java.time.Instant class
  2. Convert java.time.Instant to java.time.LocalDateTime using System's default timezone.
  3. Convert java.time.LocalDateTime to java.time.LocalDate in Java

It's easy to remember these steps once you are familiar with the new Date and Time API and how it is related to old date API. For example, the equivalent class of java.util.Date in the new API is Instant, hence you will find the toInstant() method in java.util.Date class. 

Just like old classes were a millisecond value from Epoch, Instant is also an instant in time scale but it doesn't care about time zone but LocalDatetime class uses local Timezone.

That's why when you convert an Instant to LocalDateTime, it needed a timezone. The LocalDateTime class has both date and time components, so if you just need the date part, you can use the toLocalDate() method to convert LocalDatetTime to LocalDate as shown in the following example. 

You can also see these Java 8 tutorials and courses to learn more bout the Date and time API in Java 8 and beyond. 

How to convert java.util.Date to java.time.LocalDate in Java 8 - Example




Java Program to convert java.util.Date to java.time.LocalDate

Here is our Java program to convert the java.util.Date to new java.time.LocalDate in Java 8.

package test;

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

/**
* Java Program to convert java.util.Date to java.time.LocalDate in JDK8
*/
public class Test {

public static void main(String[] args) {

// converting java.util.Date to java.time.LocalDate
Date today = new Date();
Instant instant = Instant.ofEpochMilli(today.getTime());
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant,
                                               ZoneId.systemDefault());
LocalDate localDate = localDateTime.toLocalDate();

System.out.println("java.util.Date: " + today);
System.out.println("java.time.LocalDate: " + localDate);

}

}

Output
java.util.Date: Thu Jun 09 15:02:16 PDT 2016
java.time.LocalDate: 2016-06-09

That's all about how to convert Date to LocalDate in Java. You can see that both java.util.Date and java.time.LocalDate has the same value. In the next article, we'll take a look at the opposite of this i.e. will convert the new date class to the old date class in Java 8. Please see Java SE 8 for the Really Impatient book by Cay S. Horstmann to learn more new Date and Time API of Java 8 in a quick time.


Other Java date and time tutorials you may like to explore
  • 20 Essential Examples of Date and Time API of Java 8? (tutorial)
  • How to compare two dates in Java? (tutorial)
  • How to parse String to LocalDateTime in Java 8? (tutorial)
  • How to convert Date to LocalDateTime in Java 8? (tutorial)
  • How to get the current Timestamp value in Java? (tutorial)
  • How to convert String to LocalDateTime in Java 8? (example)
  • How to parse String to Date using JodaTime library? (example)
  • How to get the current date and time in Java 6? (tutorial)
  • How to convert java.util.Date to java.sql.Date in JDBC? (tutorial)
  • How to convert java.util.Date to java.sql.Timestamp in JDBC? (tutorial)

Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions, doubts, or feedback then please drop a comment and I'll try to answer your question.

1 comment:

  1. You're making it a lot more difficult than it actually is:

    Date today = new Date();
    LocalDate localDate = today.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

    ReplyDelete