java - 为什么 LocalDateTime.ofInstant() 需要 ZoneId

在 Java 中,

  • 如果Instant表示一个时间点,编码为日期和时间 协调世界时。
  • LocalDateTime 表示一个时间点,编码为 JVM 本地时区中的日期和时间。

为什么 LocalDateTime.ofInstant() 需要一个 ZoneId 作为第二个参数?

这使得 LocalDateTime 不仅令人困惑而且可能不正确,而且与 ZonedDateTime 相同;因为 LocalDateTime 的时区可以是任何时区,就像 ZonedDateTime 一样。

最佳答案

And LocalDateTime represents a point in time, encoded as date and time in the JVM local timezone.

不,那不是真的。不管“编码为”部分(我非常怀疑,但我没有足够的知识来反驳),LocalDateTime 确实代表一个点时间。它表示本地日期/时间,不引用特定时区。任何给定的 LocalDateTime 都发生在不同时区的不同时间点。

目前欧洲/伦敦时区的本地日期和时间是 2023-01-26T08:50。 (比方说)America/New_York 中的相同时间点 会导致不同的 LocalDateTime。而在 America/New_York,2023-01-26T08:50 的 LocalDateTime 出现在不同的时间点。

对于某些 LocalDateTime/时区组合,可能会有零个或两个对应的时间点 - 例如 LocalDateTime 2022-11-06T01:30 会出现在美国/纽约,时间为 2022-11-06 05:30:00Z 和 2022-11-06 06:30:00Z。

希望这足以证明 LocalDateTime 确实不是一个时间点...

https://stackoverflow.com/questions/75243843/

相关文章:

php - 如何在 PHP 中将数字转换为字母?

amazon-ec2 - 一个 Amazon EC2 实例可以服务多少用户?

php - 过滤消息的内容上的单词黑名单

.net - Session.Timeout 和 Server.ScriptTimeout 有什么区

regex - perl中用-e和正则表达式匹配的文件名

asp.net-mvc - MVC 中的底页等价物是什么

coldfusion - 在 Coldfusion/CFML 中,如何将长十进制数格式化为标准的两位

oop - 理解对象交互的一些技巧是什么

unit-testing - TDD 如何处理异常和参数验证?

php - 静默捕获php "file_get_contents"错误