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

标签 java time local jodatime

        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/

相关文章:

java - 尝试创建一个删除按钮以从列表中删除项目

c - 用递增 100 的计数器替换 Unity Time.time?

python - 为什么在 locals 中添加 key 实际上会创建变量?

javascript - 无法在本地调试node.js

java - 子类是否具有父类的所有属性和方法?

java - 如何在 Spring MVC 中使用转发处理 Controller 之间的 ModelMap 的有效方法

java - 如何混淆基于java的项目

linux - Linux时间命令的结果

php - PHP中使用循环计算时间

css - 如何托管一个 "local"网页而不需要让它成为一个真正的网页?