This Convert Date to LocalDate and LocalDateTime Java 8 tutorial shows you my knowledge about conversion ways between the classic java.util.Date, LocalDate, and LocalDateTime. I shows aslo you how to convert LocalDate and LocalDateTime to java.util.Date.
As you know the class java.util.Date represents a specific instant in time, with millisecond precision. It means that java.util.Date doesn’t contain time zone and only represents the number of seconds passed since the Unix epoch time – 1970-01-01T00:00:00Z (midnight at the start of January 1, 1970 GMT/UTC).
Let’s begin:
Convert Date to LocalDate and LocalDateTime Java 8
Date to java.time.LocalDate
1 2 3 4 |
ZoneId defaultZoneId = ZoneId.systemDefault(); Date date = new Date(); Instant instant = date.toInstant(); LocalDate localDate = instant.atZone(defaultZoneId).toLocalDate(); |
Date to java.time.LocalDateTime
1 2 3 4 |
ZoneId defaultZoneId = ZoneId.systemDefault(); Date date = new Date(); Instant instant = date.toInstant(); LocalDateTime localDateTime = instant.atZone(defaultZoneId).toLocalDateTime(); |
Date to java.time.ZonedDateTime
1 2 3 4 |
ZoneId defaultZoneId = ZoneId.systemDefault(); Date date = new Date(); Instant instant = date.toInstant(); ZonedDateTime zonedDateTime = instant.atZone(defaultZoneId); |
Create a program for testing
We create a main class show what the above examples print out
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.javabycode.date; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Date; public class ConvertDateToJavaTime { public static void main(String[] args) { ZoneId defaultZoneId = ZoneId.systemDefault(); System.out.println("System Default TimeZone : " + defaultZoneId); Date date = new Date(); System.out.println("date : " + date); Instant instant = date.toInstant(); System.out.println("instant : " + instant); //Zone : UTC+0 LocalDate localDate = instant.atZone(defaultZoneId).toLocalDate(); System.out.println("localDate : " + localDate); LocalDateTime localDateTime = instant.atZone(defaultZoneId).toLocalDateTime(); System.out.println("localDateTime : " + localDateTime); ZonedDateTime zonedDateTime = instant.atZone(defaultZoneId); System.out.println("zonedDateTime : " + zonedDateTime); } } |
Run above main class as Java application and the output is printed out like below
Let’s look into the above output, you will see the date with time zone. This is not what we have just told the above. If you have a look on the method toString() of the Date class, you will see the system default time zone will be appended on Date object. So that Date object and time zone always are printed together.
Noticed that the new Java 8 java.time.Instant is equivalent with the classic java.util.Date
Convert LocalDate, LocalDateTime and ZonedDateTime to Date
Now, we create a main class to demonstrate how to Convert LocalDate, LocalDateTime and ZonedDateTime to java.util.Date.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.javabycode.date; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Date; public class ConvertDateToJavaTime { public static void main(String[] args) { ZoneId defaultZoneId = ZoneId.systemDefault(); LocalDate localDate = LocalDate.of(2016, 11, 2); Date date = Date.from(localDate.atStartOfDay(defaultZoneId).toInstant()); System.out.println("LocalDate -> Date"); System.out.println("localDate : " + localDate +" -> "+ "date : " + date); LocalDateTime localDateTime = LocalDateTime.of(2016,11,2,21,56,31); Date date2 = Date.from(localDateTime.atZone(defaultZoneId).toInstant()); System.out.println("LocalDateTime -> Date"); System.out.println("localDateTime : " + localDateTime + " -> "+ "date2 : " + date2); ZonedDateTime zonedDateTime = localDateTime.atZone(defaultZoneId); Date date3 = Date.from(zonedDateTime.toInstant()); System.out.println("ZonedDateTime -> Date"); System.out.println("zonedDateTime : " + zonedDateTime + " -> "+ "date3 : " + date3); } } |
Run above main as Java application and produce the output like below
That’s all on the Convert Date to LocalDate and LocalDateTime Java 8.
References
JSR 310: Date and Time API
Unix time
Instant JavaDoc