Jbartmann
Jbartmann

Reputation: 1539

Convert LocalTime (Java 8) to Date

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.

Upvotes: 20

Views: 32084

Answers (6)

Ashish M
Ashish M

Reputation: 823

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())
}

Upvotes: 0

Anonymous
Anonymous

Reputation: 86281

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

Upvotes: 2

Spyros El.
Spyros El.

Reputation: 423

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()));

Upvotes: 0

zineelabidine bakher
zineelabidine bakher

Reputation: 41

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

Upvotes: 2

Dariusz
Dariusz

Reputation: 22241

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.

Upvotes: 24

Alexandre Beaudet
Alexandre Beaudet

Reputation: 2834

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

Upvotes: 5

Related Questions