How to Convert Date to LocalDate in Java 8 - Example Tutorial

Hello guys, you may know that JDK 8 introduced the new Date and Time API, which has got a new set of shiny date classes like LocalDate, LocalTime, etc, but you still have a lot of code written against java.util.Date? In order to work with that code, you should know how to convert java.util.Date to java.util.LocalDate in Java. There are actually multiple ways to do this conversion. You can use java.sql.Date class here as well, becuase it provides direct conversion methods like the toLocalDate() and the toInstant(). Equivalent class of the java.util.Date in Java 8 is Instant.



How to convert Date to LocalDate in Java 8

Despite its name the java.util.Date represents an instant on the time-line, not a "date". The actual data stored within the object is a long count of milliseconds since 1970-01-01T00:00Z (midnight at the start of 1970 GMT/UTC).

The equivalent class to java.util.Date in JSR-310 is Instant, thus there is a convenient method like the toInstant() to provide the conversion:

Date input = new Date();
Instant instant = input.toInstant();

A java.util.Date instance has no concept of time-zone. This might seem strange if you call the toString() on a java.util.Date, because the toString is relative to a time-zone. However, that method actually uses Java's default time-zone on the fly to provide the string. The time-zone is not part of the actual state of java.util.Date.

An Instant also does not contain any information about the time-zone. Thus, to convert from an Instant to a local date it is necessary to specify a time-zone. This might be the default zone - ZoneId.systemDefault() or it might be a time-zone that your application controls, such as a time-zone from user preferences. Use the atZone() method to apply the time-zone:

Date input = new Date();
Instant instant = input.toInstant();
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());

A ZonedDateTime contains a state consisting of the local date and time, time-zone and the offset from GMT/UTC. As such the date - LocalDate - can be easily extracted using the toLocalDate(). 

If you want to learn more about Java date and time classes, I suggest you join a comprehensive Java course like The Complete Java Masterclass by Tim Buchalalaka on Udemy. It's also the most up-to-date course and covers new Java features from recent Java releases like Java 10. 

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




Java Program to Convert Date to LocalDate in Java 8

Without wasting any more of your time, here is the complete program to convert a Date object to LocalDate instance in Java 8.


package test;
 
import java.time.Instant;
import java.time.LocalDate; 
import java.time.ZoneId; 
import java.time.ZonedDateTime; 
import java.util.Date;
 
 
/**
 * Java Program to show how to convert java.util.Date and LocalDate in Java 8.
 * There are multiple ways to convert Date to LocateDate, as we see in this
 * article. 
 * 
 * @author Javin
 
 */
 
public class Test {
  
    public static void main(String args[]) {
   
        // Easiest way is by using java.sql.Date which is modified in Java 8 
        // and contains direct conversion method with new Date and Time 
        // classes e.g. toLocalDate() and toLocalTime()
 
        Date date = new java.util.Date(); 
        LocalDate localDate = new java.sql.Date(date.getTime()).toLocalDate(); 
        System.out.println("1st way to convert Date to LocalDate in Java 8 : "
                                + localDate);
   
 
        // By using Instant class op Java 8 
        Date currentDate = new Date(); 
        Instant currentInstant = currentDate.toInstant(); 
        ZonedDateTime zdt = currentInstant.atZone(ZoneId.systemDefault()); 
        LocalDate lDate = zdt.toLocalDate();
        System.out.println("2nd way to convert Date to LocalDate in Java 8 : " 
                                + lDate);
   
    }
 
} 
  
Output
 
1st way to convert Date to LocalDate in Java 8 : 2015-01-27 
2nd way to convert Date to LocalDate in Java 8 : 2015-01-27


That's all about how to convert Date to LocalDate in Java 8. In this example, I have shown you two ways to covert a given Date to LocalDate in Java, by using java.sql.Date and then by using the Instant class from Java time API.

Further Learning
The Complete Java MasterClass
Java 8 New Features in Simple Way
Java Fundamentals Part 1 and 2


Other Java 8 Date Time Tutorials you may like
  • 10 Examples of LocalDate, LocalTime, and LocalDateTime in Java (tutorial)
  • How to convert java.util.Date to java.time.LocalDateTime in Java 8? (tutorial)
  • How to convert String to LocalDateTime in Java 8? (tutorial)
  • 5 books to learn Java 8 and Functional Programming (books)
  • How to compare two Dates in Java 8? (example)
  • How to convert Timestamp to Date in Java? (example)
  • How to calculate the difference between two dates in Java? (example)
  • How to convert old Date to new LocalDate in Java 8? (example)
  • 10 Examples to format and parse Date in Java 8? (tutorial)
  • How to parse String to LocalDateTime in Java 8? (tutorial)
  • 10 Java Date, Time, and Calendar based Questions from Interviews (questions)
  • How to change the date format of String in Java 8? (tutorial)
  • 20 Examples to learn new Date and Time API in Java 8 (example)
  • 5 Free Courses to learn Java 8 and 9 (courses)
Thanks for reading this article so far, if you like this Java Date and Time tutorial then please share with your friends and colleagues. If you have any questions or feedback please drop a note.

P.S.- If you just want to learn more about new features in Java 8 then please see the course What's New in Java 8. It explains all the important features of Java 8 like lambda expressions, streams, functional interfaces, Optional, new Date Time API and other miscellaneous changes.


No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.