16

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.

Community
  • 1
  • 1
Jbartmann
  • 1,270
  • 4
  • 22
  • 39
  • Possible duplicate of [How to convert Joda time LocalTime to java.util.Date?](http://stackoverflow.com/questions/15543031/how-to-convert-joda-time-localtime-to-java-util-date) – thegauravmahawar Oct 13 '15 at 09:14
  • Possible duplicate of [Convert java.util.Date to java.time.LocalDate](http://stackoverflow.com/questions/21242110/convert-java-util-date-to-java-time-localdate) – Phantomazi Oct 13 '15 at 09:14
  • 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. – Ole V.V. Jan 03 '20 at 23:08

5 Answers5

22

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.

Dariusz
  • 19,750
  • 7
  • 65
  • 104
  • Thanks! But I'm confused by your LocalTime declaration. What does `LocalTime lt = ...` exactly mean? – Jbartmann Oct 13 '15 at 09:21
  • 1
    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. – Dariusz Oct 13 '15 at 09:51
  • 1
    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. – Basil Bourque Oct 13 '15 at 17:00
5
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

Alexandre Beaudet
  • 2,208
  • 1
  • 16
  • 28
2

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

2

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

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
0

Here is another approach:

We can add a LocalDate to the LocalTime in order to make it a LocalDateTime and then convert it to Date using the valueOf method of java.sql.Timestamp like this:

LocalTime localTime = LocalTime.now();
Date date = java.sql.Timestamp.valueOf(localTime.atDate(LocalDate.now()));
Spyros El.
  • 423
  • 3
  • 13
  • 1
    Not recommended. From [the documentation](https://docs.oracle.com/javase/10/docs/api/java/sql/Timestamp.html): “it is recommended that code not view `Timestamp` values generically as an instance of `java.util.Date`.” – Ole V.V. Jan 04 '20 at 00:14