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
OUTPUT of the above code
Explanation of the code
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);
}
}
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
- The above example starts with creating a new
java.util.Dateinstance, namedjava7Date. This creates an instance ofDatecontaining date, time and time zone as per the system on which the JVM is running. - Next we convert
java7Dateto an equivalent instance ofjava.time.LocalDateTime. This conversion is done in 3-steps -java7Date.toInstant()is invoked. This returns an instance ofjava.time.Instantwhich contains the date,time, and time zone extracted fromjava7Date.Instant.atZone(ZoneId zoneId)method is used, with the value forZoneIdpassed asZoneId.systemDefault(). TheZoneId.systemDefault()method internally reads the time zone of the system clock on the machine on which JVM is running. This method returns ajava.time.ZonedDateTimeinstance. If you are new toZoneIdandZonedDateTimethen you can read the detailed tutorial on Java 8 time zonesClick to read Java 8 Time Zones tutorial.- Next
ZonedDateTime.toLocalDateTime()method is invoked on theZonedDateTimeinstance obtained in previous step. This gives us the desiredjava.time.LocalDateTimeinstance which is equivalent tojava7Date.
- Getting
LocalDatefromLocalDateTimeis straightforward, and is accomplished using theLocalDateTime.toLocalDate()method. - The values for
java7Datealong with equivalentLocalDateandLocalDateTimevalues are printed, and as expected they all match.
Tutorials on Java 8’s new Date and Time API
Overview of Java 8's new Date and Time APIClick to Read Overview of Java 8's new Date-Time API Working with time zones in Java 8| ZonedDateTime, ZoneId tutorial with examplesClick to Read tutorial on time zone handling in Java 8 How to convert LocalDate to String and String to LocalDateClick to Read tutorial on String to LocalDate conversions How to convert java.util.Date to java.time.LocalDateClick to Read tutorial on java.util.Date to LocalDate conversion Formatting localized dates in Spanish and FrenchClick to Read date formatting in Spanish & French How to get day-of-week for a given dateHow to get day-of-week using java.time.DayOfWeek enumDate Modification using TemporalAdjuster Click to Read Tutorial on TemporalAdjusters
Overview of Java 8's new Date and Time APIClick to Read Overview of Java 8's new Date-Time API Working with time zones in Java 8| ZonedDateTime, ZoneId tutorial with examplesClick to Read tutorial on time zone handling in Java 8 How to convert LocalDate to String and String to LocalDateClick to Read tutorial on String to LocalDate conversions How to convert java.util.Date to java.time.LocalDateClick to Read tutorial on java.util.Date to LocalDate conversion Formatting localized dates in Spanish and FrenchClick to Read date formatting in Spanish & French How to get day-of-week for a given dateHow to get day-of-week using java.time.DayOfWeek enumDate Modification using TemporalAdjuster Click to Read Tutorial on TemporalAdjusters