Convert LocalTime (Java 8) to Date

I am trying to convert a java.time.LocalTime object to java.util.Date but cannot find a suitable method. What is the right way to do this?

Is there some reason Java doesn't seem to come with a built-in direct conversion method?

Possible duplicates:
How to convert joda time - Doesn't work for me, maybe I missed some joda libraries?
How to convert Date to LocalTime? - it converts addresses the other way around.

+7
java date java-8 java-time
source share
2 answers

LocalTime cannot actually be converted to Date, because it contains only the temporary part of DateTime. Like 11:00. But not a day is known. You must put it manually:

 LocalTime lt = ...; Instant instant = lt.atDate(LocalDate.of(A_YEAR, A_MONTH, A_DAY)). atZone(ZoneId.systemDefault()).toInstant(); Date time = Date.from(instant); 

Here's a blog post explaining all the conversions between the new and old APIs.

There is no simple built-in conversion method, since these APIs approach the idea of โ€‹โ€‹date and time in a completely different way.

+11
source share
 LocalTime lt = ...; Instant instant = lt.atDate(LocalDate.of(A_YEAR, A_MONTH, A_DAY)). atZone(ZoneId.systemDefault()).toInstant(); Date time = Date.from(instant); 

From: http://blog.progs.be/542/date-to-java-time

+2
source share

All Articles