GWT DateTimeFormat反转时区值

Zal*_*aka 7 java gwt timezone datetime date

考虑以下代码在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))); 
Run Code Online (Sandbox Code Playgroud)

假设它是格林威治时间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
Run Code Online (Sandbox Code Playgroud)

预期的是

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
Run Code Online (Sandbox Code Playgroud)

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

Chr*_*her 8

"Etc/GMT-2"实际上(并且非常令人惊讶地)"+02:00",请参阅http://en.wikipedia.org/wiki/Tz_database#Area:

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

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

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
Run Code Online (Sandbox Code Playgroud)

所以,它不是DateTimeFormat负责倒车的原因,而是TimeZone.createTimeZone(int timeZoneOffsetInMinutes)!


让我们看一下com.google.gwt.i18n.client.TimeZone的GWT javadocs:

getOffset(Date 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.
Run Code Online (Sandbox Code Playgroud)

并且composeGMTString(int offset):

* In GMT representation, +/- has reverse sign of time zone offset.
* when offset == 480, it should output GMT-08:00.
Run Code Online (Sandbox Code Playgroud)