Java 8 – How to convert java.util.Date to java.time.LocalDate, java.time.LocalDateTime with examples

This quick Java 8 coding tip shows how to convert java.util.Date to java.time.LocalDate, along with conversion from java.util.Date to java.time.LocalDateTime. This tip is especially useful for those who are migrating their old code in Java 7 to Java 8 code, while some of the external libraries being used are still in Java 7 and hence returning java.util.Date values. It is recommended that Java 8 code should use Date-Time classes from the new java.time package and not the earlier java.util based date classes.

Java 8 code to convert java.util.Date to java.time.LocalDate & java.time.LocalDateTime
package com.javabrahman.java8.time;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class InstantToLocalDateTime {
  public static void main(String args[]){
    Date java7Date=new Date();
    LocalDateTime localDateTime=java7Date.toInstant()
                                         .atZone(ZoneId.systemDefault())
                                         .toLocalDateTime();
    LocalDate localDate=localDateTime.toLocalDate();
    System.out.println("java.util.Date value:"+java7Date);
    System.out.println("Equivalent java.time.LocalDate value:"+localDate);
    System.out.println("Equivalent java.time.LocalDateTime value:"+localDateTime);
  }
}
 OUTPUT of the above code
java.util.Date value :   Wed Nov 30 16:18:26 IST 2016
Equivalent java.time.LocalDate value :   2016-11-30
Equivalent java.time.LocalDateTime value :   2016-11-30T16:18:26.975
Explanation of the code

  • The above example starts with creating a new java.util.Date instance, named java7Date. This creates an instance of Date containing date, time and time zone as per the system on which the JVM is running.
  • Next we convert java7Date to an equivalent instance of java.time.LocalDateTime. This conversion is done in 3-steps –
    1. java7Date.toInstant() is invoked. This returns an instance of java.time.Instant which contains the date,time, and time zone extracted from java7Date.
    2. Instant.atZone(ZoneId zoneId) method is used, with the value for ZoneId passed as ZoneId.systemDefault(). The ZoneId.systemDefault() method internally reads the time zone of the system clock on the machine on which JVM is running. This method returns a java.time.ZonedDateTime instance. If you are new to ZoneId and ZonedDateTime then you can read the detailed tutorial on Java 8 time zonesClick to read Java 8 Time Zones tutorial.
    3. Next ZonedDateTime.toLocalDateTime() method is invoked on the ZonedDateTime instance obtained in previous step. This gives us the desired java.time.LocalDateTime instance which is equivalent to java7Date.
  • Getting LocalDate from LocalDateTime is straightforward, and is accomplished using the LocalDateTime.toLocalDate() method.
  • The values for java7Date along with equivalent LocalDate and LocalDateTime values are printed, and as expected they all match.

 

Digiprove sealCopyright © 2014-2019 JavaBrahman.com, all rights reserved.