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

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

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

问题

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

  1. LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
  2. ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of(timeZone));
  3. 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?

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

答案1

得分: 4

  1. # 简而言之
  2. myJavaUtilDate.toInstant()
  3. 不需要使用 `LocalDateTime``ZoneId``ZonedDateTime` 类。
  4. # 详情
  5. 你说:
  6. >我有一个 `Date` 类的实例
  7. java.util.Date myJavaUtilDate = ;
  8. 这个类现在已经过时,多年前被 JSR 310 中定义的现代 *java.time* 类所取代。具体而言,被 `java.time.Instant` 取代,用于表示一个时刻,时间轴上的特定点,与 UTC 时区的时间子午线相隔零小时-零分-零秒。
  9. >以及一个时区(例如 Europe/London
  10. ZoneId z = ZoneId.of( "Europe/London" ) ;
  11. > 如何将其转换为 Instant
  12. 不需要时区。`java.util.Date` `java.time.Instant` 都表示在 UTC 中看到的时刻,如上所述。
  13. *旧* 类添加了新的 👉 **转换方法**。查找 `to…` `from…` 的[命名约定][1]。你可以在旧类和现代类之间相互转换。坚持使用现代类。只有在与尚未更新为 *java.time* 的旧代码进行交互时才使用旧的日期时间类。
  14. java.time.Instant instant = myJavaUtilDate.toInstant() ;
  15. 另一个方向:
  16. java.util.Date d = java.util.Date.from( instant ) ;
  17. 在这个方向上要注意数据丢失。`Instant` 的分辨率为纳秒,而 `java.util.Date` 的分辨率为毫秒。
  18. [1]: https://docs.oracle.com/javase//tutorial/datetime/overview/naming.html
英文:

tl;dr

  1. myJavaUtilDate.toInstant()

No need for LocalDateTime, ZoneId, ZonedDateTime classes.

Details

You said:

>I have an instance of the Date class

  1. 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)

  1. 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.

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

And the other direction:

  1. 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:

确定