gpt4 book ai didi

java - GWT DateTimeFormat 反转时区值

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:21:22 25 4
gpt4 key购买 nike

考虑以下代码在 GWT 中运行:

import com.google.gwt.i18n.client.DateTimeFormat;
...
DateTimeFormat fullDateTimeFormat = DateTimeFormat.getFullDateTimeFormat();
Log.info(fullDateTimeFormat.format(date, TimeZone.createTimeZone(-120)));
Log.info(fullDateTimeFormat.format(date, TimeZone.createTimeZone(0)));
Log.info(fullDateTimeFormat.format(date, TimeZone.createTimeZone(180)));

假设现在是格林威治时间 16:00。

为什么我会得到以下输出?

Monday, February 21, 2011 6:00:00 PM Etc/GMT-2
Monday, February 21, 2011 4:00:00 PM Etc/GMT
Monday, February 21, 2011 1:00:00 PM Etc/GMT+3

预期的是

Monday, February 21, 2011 2:00:00 PM Etc/GMT-2
Monday, February 21, 2011 4:00:00 PM Etc/GMT
Monday, February 21, 2011 7:00:00 PM Etc/GMT+3

什么是正确的修复方法?

最佳答案

“Etc/GMT-2”实际上(非常令人惊讶)是“+02:00”,参见 http://en.wikipedia.org/wiki/Tz_database#Area :

In order to conform with the POSIX style, those zones beginning with "Etc/GMT" have their sign reversed from what most people expect. In this style, zones west of GMT have a positive sign and those east have a negative sign.

您的代码在我的机器上导致不同的输出(可能是因为我的语言环境不同):

Monday, 2011 February 21 18:00:00 UTC+2
Monday, 2011 February 21 16:00:00 UTC
Monday, 2011 February 21 13:00:00 UTC-3

所以,负责反转的不是DateTimeFormat,而是TimeZone.createTimeZone(int timeZoneOffsetInMinutes)!


让我们进一步了解 com.google.gwt.i18n.client.TimeZone 的 GWT javadoc:

getOffset(Date 日期):

* Returns the RFC representation of the time zone name for the given date.
* To be consistent with JDK/Javascript API, west of Greenwich will be
* positive.

composeGMTString(int offset):

* In GMT representation, +/- has reverse sign of time zone offset.
* when offset == 480, it should output GMT-08:00.

关于java - GWT DateTimeFormat 反转时区值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5068701/

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