如何将Java Date转换为给定时区的Instant?

huangapple go评论52阅读模式
英文:

How do I convert a Java Date into an Instant for a given timezone?

问题

我有一个Date类的实例和一个时区(例如Europe/London)。我如何将其转换为Instant?以下是正确的方法吗?

LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of(timeZone));
Instant instant = zonedDateTime.toInstant();
英文:

I have an instance of the Date class and a time zone (e.g Europe/London). How do I convert this into an Instant? Is the below the correct way of doing this?

LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of(timeZone));
Instant instant = zonedDateTime.toInstant();

答案1

得分: 4

# 简而言之

    myJavaUtilDate.toInstant()

不需要使用 `LocalDateTime`、`ZoneId`、`ZonedDateTime` 类。

# 详情

你说:

>我有一个 `Date` 类的实例

    java.util.Date myJavaUtilDate = … ;

这个类现在已经过时,多年前被 JSR 310 中定义的现代 *java.time* 类所取代。具体而言,被 `java.time.Instant` 取代,用于表示一个时刻,时间轴上的特定点,与 UTC 时区的时间子午线相隔零小时-零分-零秒。

>以及一个时区(例如 Europe/London)

    ZoneId z = ZoneId.of( "Europe/London" ) ;
    
> 如何将其转换为 Instant?

不需要时区。`java.util.Date` 和 `java.time.Instant` 都表示在 UTC 中看到的时刻,如上所述。

*旧* 类添加了新的 👉 **转换方法**。查找 `to…``from…` 的[命名约定][1]。你可以在旧类和现代类之间相互转换。坚持使用现代类。只有在与尚未更新为 *java.time* 的旧代码进行交互时才使用旧的日期时间类。

    java.time.Instant instant = myJavaUtilDate.toInstant() ;

另一个方向:

    java.util.Date d = java.util.Date.from( instant ) ;

在这个方向上要注意数据丢失。`Instant` 的分辨率为纳秒,而 `java.util.Date` 的分辨率为毫秒。


  [1]: https://docs.oracle.com/javase//tutorial/datetime/overview/naming.html
英文:

tl;dr

myJavaUtilDate.toInstant()

No need for LocalDateTime, ZoneId, ZonedDateTime classes.

Details

You said:

>I have an instance of the Date class

java.util.Date myJavaUtilDate = … ;

This class is now legacy, years ago supplanted by the modern java.time classes defined in JSR 310. Specifically replaced by java.time.Instant, to represent a moment, a specific point on the timeline, as seen with an offset of zero hours-minutes-seconds from the temporal meridian of UTC.

>and a time zone (e.g Europe/London)

ZoneId z = ZoneId.of( "Europe/London" ) ;

> How do I convert this into an Instant?

No need for the time zone. Both java.util.Date and java.time.Instant represent a moment as seen in UTC, as described above.

New 👉 conversion methods were added to the old classes. Look for the naming conventions of to… & from…. You can convert back and forth between the legacy classes and the modern classes. Stick to the modern classes. Use the legacy date-time classes only to interoperate with old code not yet updated to java.time.

java.time.Instant instant = myJavaUtilDate.toInstant() ;

And the other direction:

java.util.Date d = java.util.Date.from( instant ) ;

Beware of data loss in this other direction. An Instant has a resolution of nanoseconds, while java.util.Date resolves to milliseconds.

huangapple
  • 本文由 发表于 2023年5月25日 15:20:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定