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

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 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 Java 8 conversion tutorial series where I'll share lots of tutorial about converting old date classes to new date and time classes in Java 8.




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

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 new Date and Time API and how it is related to old date API. For example, the equivalent class of java.util.Date in new API is Instant, hence you will find the toInstant() method in java.util.Date class. Just like old classes was 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 component, so if you just need date part, you can use the toLocalDate() method to convert LocalDatetTime to LocalDate as shown in the following the 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

You can see that both java.util.Date and java.time.LocalDate has the same value. In next article, we'll take a look at the opposite of this i.e. will convert new date class to old date class in Java 8. Please see Java SE 8 for Really Impatient by Cay S. Horstmann to learn more new Date and Time API of Java 8 in 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 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 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)

References


1 comment :

Erik C. Thauvin said...

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

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

Post a Comment