繁体   English   中英

如何将 joda DateTime 与时区转换为 DateTime 与 UTC 相同的时间?

[英]How to convert joda DateTime with timezone to DateTime with same time in UTC?

我有这样的输入日期:

DateTime date = new DateTime("2022-10-30T00:00:00.000+11:00");

如何在保持相同时间的同时将其转换为 UTC: "2022-10-30 00:00:00.000"

换句话说,我想要date.getMillis(); 方法以 UTC 返回“2022-10-30”的午夜。

现在,如果我调用date.getMillis(); ,我在 UTC 中得到“2020-10-29”。

我不确定你在做你认为你在这里做的事情。

您创建的日期没有 UTC+11 偏移量,它位于 JVM 的默认时区 ( demo ):

System.out.println(DateTimeZone.getDefault());
DateTime date = new DateTime("2022-10-30T00:00:00.000+11:00");
System.out.println(date);

印刷

Etc/UTC
2022-10-29T13:00:00.000Z

所以它的本地日期部分是2022-10-29

如果要使用指定的偏移量创建它,则必须使用DateTime.parse

DateTime date = DateTime.parse("2022-10-30T00:00:00.000+11:00");

或指定它:

DateTime date = new DateTime("2022-10-30T00:00:00.000+11:00", DateTimeZone.forOffsetHours(11));

打印这些结果中的任何一个:

2022-10-30T00:00:00.000+11:00

现在, date.withZoneRetainFields(DateTimeZone.UTC)是:

2022-10-30T00:00:00.000Z

按要求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM