gpt4 book ai didi

java - 初始化 LocalDateTime - 时间为零

转载 作者:行者123 更新时间:2023-11-30 11:06:28 29 4
gpt4 key购买 nike

Initalize LocalDateTime - 零计时的正确方法是什么

我正在使用方法 withTime(0, 0, 0, 0) 实现看起来像 new LocalDateTime().withTime(0, 0, 0, 0);

所以我的问题是,在处理DayLight SavingsTimezone anamolies 时,这种初始化方式是否会导致任何问题。或者是否有任何其他选项可以避免这些问题。

提前致谢

最佳答案

如果您在 LocalDateTime 的范围内,那么您将不会遇到夏令时或其他时区转换的任何问题。 LocalDateTime 不了解与时区相关的事情。但是:

如果您将任何 LocalDateTime 转换为 DateTime 实例,该实例引用了全局时间线,那么您可能会遇到问题。相关问题的描述可以在 javadoc 中找到。 .还解释了弃用某些方法的原因(巴西通常在午夜开始夏令时,因此在午夜创建 LocalDateTime 不是问题,但创建 DateTime ).

如果您想知道如何安全地将 LocalDateTime(任何时间组件可能落入间隙)转换为(全局)DateTime,那么 Joda-Time 可以不幸的是,没有提供任何冲突策略。 transition strategies Joda-Time 应用是固定的且不可配置的:

When the time zone is applied, the local date-time may be affected by daylight saving. In a daylight saving gap, when the local time does not exist, this method will throw an exception. In a daylight saving overlap, when the same local time occurs twice, this method returns the first occurrence of the local time.

但是,可能存在针对此限制的一些变通方法。例如,如果给定的 LocalDateTime falls in a gap,您可以询问相关的 DateTimeZone .根据此查询的答案,您可以决定要应用哪个偏移量以转换为 DateTime

关于java - 初始化 LocalDateTime - 时间为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29295424/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com