首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GWT DateTimeFormat反转时区值

GWT DateTimeFormat反转时区值
EN

Stack Overflow用户
提问于 2011-02-22 00:47:33
回答 2查看 3.1K关注 0票数 7

假设以下代码在GWT中运行:

代码语言:javascript
复制
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。

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

代码语言:javascript
复制
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

预期的是

代码语言:javascript
复制
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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-22 01:40:16

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

为了符合POSIX风格,那些以"Etc/GMT“开头的区域的标志与大多数人的预期相反。在这种风格中,GMT以西的区域有积极的迹象,而东部的区域有负面的迹象。

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

代码语言:javascript
复制
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)

代码语言:javascript
复制
* 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)

代码语言:javascript
复制
* In GMT representation, +/- has reverse sign of time zone offset.
* when offset == 480, it should output GMT-08:00.
票数 8
EN

Stack Overflow用户

发布于 2011-02-22 01:18:27

我看了一下源代码。它有评论说

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

都一样。根据这一点,我推断时间和时区之间的关系是从GMT中减去或添加到GMT中的时间量。因此,1600GMT变成1400GMT -0200或1900GMT +0300。记住这一点,我们必须以另一种方式工作,以获得所需的结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5068701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档