Convert LocalTime (Java 8) to Date
Asked Answered
C

6

20

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.

Christmas answered 13/10, 2015 at 9:11 Comment(3)
Possible duplicate of How to convert Joda time LocalTime to java.util.Date?Spirula
Possible duplicate of Convert java.util.Date to java.time.LocalDateRf
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.Kerrikerrie
E
24

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.

Engineman answered 13/10, 2015 at 9:16 Comment(3)
Thanks! But I'm confused by your LocalTime declaration. What does LocalTime lt = ... exactly mean?Christmas
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.Engineman
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.Delict
P
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

Piggyback answered 13/10, 2015 at 9:14 Comment(0)
B
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

Bondie answered 4/11, 2019 at 9:28 Comment(0)
K
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

Kerrikerrie answered 3/1, 2020 at 23:21 Comment(2)
LocalDate.EPOCH don't seem to be around in java 8 at leastFaunia
@Faunia True, LocalDate.EPOCH was introduced in java 9. In Java 8 I believe you may use LocalDate.ofEpochDay(0), for example (and maybe declare your own constant with that value).Kerrikerrie
R
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()));
Refractory answered 3/1, 2020 at 10:4 Comment(1)
Not recommended. From the documentation: “it is recommended that code not view Timestamp values generically as an instance of java.util.Date.”Kerrikerrie
T
0

As @Dariusz said, we cannot convert LocalTime to Date directly as it contains only time part but Date must contain all the value along with the timeZone.

In order to get the date part, we can use LocalDate.now(). It will give us LocalDate object with today's date.

Now, we have both LocalDate and LocalTime, we can now use the LocalDateTime.of(date: LocalDate, time: LocalTime) or localTime.atDate(date: LocalDate) to get the LocalDateTime object. And now we can convert the LocalDateTime to Date using below kotlin extension function.

fun LocalDateTime.toDate(): Date {
    return Date.from(this.atZone(ZoneId.systemDefault()).toInstant())
}
Tautog answered 8/8, 2022 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.