Convert LocalTime (Java 8) to Date

24,087

Solution 1

LocalTime actually can't be converted to a Date, because it only contains the time part of DateTime. Like 11:00. But no day is known. You have to supply 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 which explains all the conversions between the new and the old API.

There's no simple built-in conversion method, because these APIs approach the idea of date and time in completely different way.

Solution 2

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

Solution 3

I added the data (hour, minute, second) one by one (from localtime to date):

reta.setHours(vol.getRetard().getHour());
reta.setMinutes(vol.getRetard().getMinute());
reta.setSeconds(vol.getRetard().getSecond());

Note : reta: Date veriabble ; vol.getRetard (): localtime variable

Solution 4

As others have said, it’s a problematic question in that a LocalTime and a Date really represent quite different and almost unrelated concepts. A LocalTime is a time of day without time zone, such as 19:45 (or 7:45 PM). A Date is a point on the time line; if it happens to coincide with 19:45 on some date in some time zone, it will not in other time zones.

I believe that the conventional way of misusing (indeed) a Date for an hour of day is setting it to that time of day on January 1, 1970 in the default time zone of the JVM. This practice carries all of the liabilities already mentioned. In particular the JVM default time zone setting can be changed at any time from another part of your program or any other program running in the same JVM. This means that a completely unrelated program may suddenly cause your Date to indicate a different time of day than the one you had initialized it to.

There’s nothing better we can do, so here goes:

    LocalTime time = LocalTime.of(11, 0);

    Instant timeOnEpochDayInDefaultTimeZone = LocalDate.EPOCH
            .atTime(time)
            .atZone(ZoneId.systemDefault())
            .toInstant();
    Date oldfashionedDateObject = Date.from(timeOnEpochDayInDefaultTimeZone);

    System.out.println(oldfashionedDateObject);

In my time zone output from this snippet is:

Thu Jan 01 11:00:00 CET 1970

Share:
24,087

Related videos on Youtube

Jbartmann
Author by

Jbartmann

Web Developer

Updated on July 09, 2022

Comments

  • Jbartmann
    Jbartmann almost 2 years

    I'm trying to convert a java.time.LocalTime object to java.util.Date but can't find any suitable method. What's the correct way to do this?

    Is there any reason why java doesn't seem to ship with a built-in direct conversion method?

    To possible duplicates:
    How to convert joda time - Doesn't work for me, probably I'm missing some "joda" libraries?
    How to convert Date to LocalTime? - This adresses conversion the other way around.

  • Jbartmann
    Jbartmann over 8 years
    Thanks! But I'm confused by your LocalTime declaration. What does LocalTime lt = ... exactly mean?
  • Dariusz
    Dariusz over 8 years
    It means that you should manually create the object or get it from somewhere. It's not Java, if that's what you're asking.
  • Basil Bourque
    Basil Bourque over 8 years
    Dariusz means you must provide four things to use his code: a LocalTime object, an integer for the year, an integer for the month, and an integer for the day. Also, replace the ZoneId.systemDefault with a specific ZoneId depending on where you intended this date-time. 12:30 on Dec 3rd comes earlier in Paris than Montreal for example.
  • Ole V.V.
    Ole V.V. over 4 years
    Not recommended. From the documentation: “it is recommended that code not view Timestamp values generically as an instance of java.util.Date.”