Java GWT DateTimeFormat反转时区值

Java GWT DateTimeFormat反转时区值,java,datetime,gwt,date,timezone,Java,Datetime,Gwt,Date,Timezone,考虑以下代码在GWT中运行: import com.google.gwt.i18n.client.DateTimeFormat; ... DateTimeFormat fullDateTimeFormat = DateTimeFormat.getFullDateTimeFormat(); Log.info(fullDateTimeFormat.format(date, TimeZone.createTimeZone(-120))); Log.info(fullDateTimeFormat.for

考虑以下代码在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点

为什么我会得到以下输出

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

什么是修复它的正确方法?

我查看了源代码。有评论说

20:00 GMT0000或16:00 GMT-0400或12:00 GMT-0800

都一样。根据这一点,我推断出时间和时区之间的关系,即从GMT减去或加到GMT的时间量。因此,16.00 GMT变为1400 GMT-0200或1900 GMT+0300。记住这一点,我们必须以另一种方式来获得期望的结果。

“Etc/GMT-2”实际上是(非常令人惊讶的)“+02:00”,请参见:

为了符合POSIX风格,那些以“Etc/GMT”开头的区域的符号与大多数人的预期相反。在这种风格中,格林尼治标准时间以西的区域有一个正符号,以东的区域有一个负符号

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

因此,负责反转的不是
DateTimeFormat
,而是
TimeZone.createTimeZone(int timeZoneOffsetInMinutes)


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

getOffset(日期)

composeGMTString(int offset)

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
* 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.
* In GMT representation, +/- has reverse sign of time zone offset.
* when offset == 480, it should output GMT-08:00.