gpt4 book ai didi

java - Joda Time : Convert local to UTC ambiguity

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:46:49 24 4
gpt4 key购买 nike

我正在尝试使用 Joda Time 将本地日期转换为 UTC .我使用的代码如下所示,效果很好。

Date localDate = new Date();
System.out.println("Local Date : " + localDate);

DateTimeZone tz = DateTimeZone.getDefault();
Date utcDate = new Date(tz.convertLocalToUTC(localDate.getTime(), false));
System.out.println("UTC Date : " + utcDate);

Output :
Local Date : Wed May 29 11:54:46 EEST 2013
UTC Date : Wed May 29 08:54:46 EEST 2013

但是,如果我将 UTC 日期作为参数发送给 DateTimeZone.convertLocalToUTC()方法,它还会将小时减少 3。但是,由于它是 UTC 日期,我希望它不会再次转换日期。这是错误还是我遗漏了什么?

Date localDate = new Date();
System.out.println("Local Date : " + localDate);

DateTimeZone tz = DateTimeZone.getDefault();
Date utcDate = new Date(tz.convertLocalToUTC(localDate.getTime(), false));
System.out.println("UTC Date : " + utcDate);

Date utcDate2 = new Date(tz.convertLocalToUTC(utcDate.getTime(), false));
System.out.println("UTC Date 2 : " + utcDate2);

Output :
Local Date : Wed May 29 11:54:46 EEST 2013
UTC Date : Wed May 29 08:54:46 EEST 2013
UTC Date 2 : Wed May 29 05:54:46 EEST 2013

最佳答案

根据 convertLocalToUTC 的 javadoc

Converts a local instant to a standard UTC instant with the same local time. This conversion is used after performing a calculation where the calculation was done using a simple local zone.

Methods 不假设或验证传递日期是否为 UTC,它始终将传递日期视为本地日期并转换为 UTC。你的程序输出是正确的。

关于java - Joda Time : Convert local to UTC ambiguity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16809802/

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