假设以下代码在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修复它的正确方法是什么?
发布于 2011-02-22 01:40:16
"Etc/GMT-2“实际上(非常令人惊讶)是"+02:00",参见http://en.wikipedia.org/wiki/Tz_database#Area
为了符合POSIX风格,那些以"Etc/GMT“开头的区域的标志与大多数人的预期相反。在这种风格中,GMT以西的区域有积极的迹象,而东部的区域有负面的迹象。
您的代码在我的机器上导致了不同的输出(可能是因为我的语言环境不同):
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 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.发布于 2011-02-22 01:18:27
我看了一下源代码。它有评论说
20:00 GMT0000,或16:00 GMT-0400,或12:00 GMT-0800
都一样。根据这一点,我推断时间和时区之间的关系是从GMT中减去或添加到GMT中的时间量。因此,1600GMT变成1400GMT -0200或1900GMT +0300。记住这一点,我们必须以另一种方式工作,以获得所需的结果。
https://stackoverflow.com/questions/5068701
复制相似问题