gpt4 book ai didi

java - 在 Joda 中将 UTC 转换为 LocalDateTime?

转载 作者:行者123 更新时间:2023-12-01 18:25:40 26 4
gpt4 key购买 nike

        DateTime dt = new DateTime("2014-09-15T21:20:14");
System.out.println(dt);
System.out.println(dt.plusMillis(581042272).toDateTime().toLocalDateTime().toDateTime(DateTimeZone.forID("GMT")));

dt 中的时间是 UTC,我想将 dt 中的时间加上毫秒设置为 GMT?但是,时间仍显示为 UTC(比 GMT 晚 1 小时)。我怎样才能将其设置为提前一小时?

2014-09-15T21:20:14.000+01:00
2014-09-22T14:44:16.272Z

我知道时间正好晚了一小时,因为我在 15:44:16 GMT 提出了此请求

最佳答案

您的DateTime实际上不是 UTC - 它位于系统默认时区。要修复此问题,您只需告诉它您传入的值采用 UTC 格式:

DateTime dt = new DateTime("2014-09-15T21:20:14", DateTimeZone.UTC);
System.out.println(dt);
DateTime other = dt.plusMillis(581042272);
System.out.println(other);

输出:

2014-09-15T21:20:14.000Z
2014-09-22T14:44:16.272Z

另请注意,您无法在格林尼治标准时间 15:44:16 提出请求,因为这种情况尚未发生。在我写这篇文章时,英国夏令时间为 16:05,因此格林尼治标准时间为 15:05。重要的是要了解英国的时区不是“GMT” - 这只是我们不遵守夏令时时区的一部分。

如果您想转换为英国时区,您需要:

DateTime other = dt.plusMillis(581042272)
.withZone(DateTimeZone.forID("Europe/London"));

关于java - 在 Joda 中将 UTC 转换为 LocalDateTime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60237066/

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