将本地时间 (Java 8) 转换为日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33098603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:11:05  来源:igfitidea点击:

Convert LocalTime (Java 8) to Date

javadatejava-8java-time

提问by Jbartmann

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

我正在尝试将java.time.LocalTime对象转换为,java.util.Date但找不到任何合适的方法。这样做的正确方法是什么?

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

java似乎没有内置直接转换方法有什么原因吗?

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.

对于可能的重复:
如何转换 joda 时间- 对我不起作用,可能我缺少一些“joda”库?
如何将日期转换为本地时间?- 这以相反的方式处理转换。

回答by Dariusz

LocalTime actually can't be converted to a Date, because it only contains the timepart of DateTime. Like 11:00. But no day is known. You have to supply it manually:

LocalTime实际上并不能转换成Date,因为它只包含DateTime的时间部分。像 11:00。但不知道哪一天。您必须手动提供它:

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 conversionsbetween the new and the old API.

这是一篇博客文章,解释了新旧 API 之间的所有转换

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

没有简单的内置转换方法,因为这些 API 以完全不同的方式处理日期和时间的概念。

回答by Alexandre Beaudet

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

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

回答by zineelabidine bakher

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

注:reta:日期可验证;vol.getRetard():本地时间变量

回答by Ole V.V.

As others have said, it's a problematic question in that a LocalTimeand a Datereally represent quite different and almost unrelated concepts. A LocalTimeis a time of day without time zone, such as 19:45 (or 7:45 PM). A Dateis 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.

正如其他人所说,这是一个有问题的问题,因为 aLocalTime和 aDate确实代表了完全不同且几乎不相关的概念。ALocalTime是一天中没有时区的时间,例如 19:45(或晚上 7:45)。ADate是时间线上的一个点;如果它恰好与某个时区某个日期的 19:45 重合,则不会在其他时区。

I believe that the conventional way of misusing (indeed) a Datefor 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 Dateto indicate a different time of day than the one you had initialized it to.

我相信滥用(实际上)一个Date小时的传统方法是将它设置为 1970 年 1 月 1 日在 JVM 的默认时区中的那个时间。这种做法承担了已经提到的所有责任。特别是 JVM 默认时区设置可以随时从程序的另一部分或在同一 JVM 中运行的任何其他程序进行更改。这意味着一个完全不相关的程序可能会突然导致您Date指示一天中与您初始化时不同的时间。

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

1970 年 1 月 1 日星期四 11:00:00 CET

回答by Spyros El.

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 添加一个 LocalDate 以使其成为 LocalDateTime,然后使用 java.sql.Timestamp 的 valueOf 方法将其转换为 Date,如下所示:

LocalTime localTime = LocalTime.now();
Date date = java.sql.Timestamp.valueOf(localTime.atDate(LocalDate.now()));