3

What is the best way to convert from java.time.LocalDate to java.util.Date ?

Date.from(dateToReturn.atStartOfDay(ZoneId.systemDefault()).toInstant()

I have been trying this one but does not seem to work correct with the time although converts the date month and year correctly.

Update : java.time.LocalDate does not save time informations Just used java.time.LocalDateTime instead and everything works fine.

Nikos
  • 387
  • 2
  • 15

1 Answers1

7
LocalDate ld = ...;
Instant instant = ld.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
Date res = Date.from(instant);

Check out this blog post, Converting between Date and java8 java.time.LocalDateTime, LocalDate and LocalTime by joachim.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
brso05
  • 13,142
  • 2
  • 21
  • 40
  • The link gave me the answer ! My (Stupid) mistake was that I assummed that java.time.LocalDate is the equivalent of java util Date while I need to use java.time.LocalDateTime cause LocalDate does not save time informations ! Thanks anyway ! – Nikos Dec 02 '14 at 14:13