Java / GWT 中的时区(客户端)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2155469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:44:28  来源:igfitidea点击:

Time Zones in Java / GWT (Client-side)

javagwt

提问by Federer

[Client-side GWT class]

[客户端 GWT 类]

I have a Date Object...

我有一个日期对象...

Date dataObject = DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ss.SSS")
                      .parse("2009-10-12T00:00:00.000);

This works fine. However when I do a:

这工作正常。但是,当我执行以下操作时:

dateObject.getTime();

It returns a UNIX Time milliseconds using a GMT with daylight savings, therefore making it a UNIX Time I cannot use. I need it in UTC. How do I do this?

它使用具有夏令时的 GMT 返回 UNIX 时间毫秒,因此使其成为我无法使用的 UNIX 时间。我需要它在 UTC 中。我该怎么做呢?



Currently I'm parsing a date and it is giving me back:

目前我正在解析一个日期,它给了我回报:

'Thu Apr 16 08:46:20 GMT+100 2009' @ '1239867980191'

However the date I'm passing in is 1 hour less than this time (7:46 and not 8:46!).

然而,我经过的日期比这个时间少 1 小时(7:46 而不是 8:46!)。

How do I pass in the fact it's UTC? Or if it can't use UTC (which would be ridiculous), how do I use GMT without the daylight savings?

我如何传递它是 UTC 的事实?或者,如果它不能使用 UTC(这很荒谬),我如何在没有夏令时的情况下使用 GMT?

采纳答案by Thomas Pornin

Your last edit makes things clearer.

您上次的编辑使事情变得更清晰。

Basically, you are confused, and you already get what you want.

基本上,你很困惑,你已经得到了你想要的。

1239867980191 milliseconds since the Epoch translates to Thursday, April 16th, 2009, at 7:46:20.191 in the GMT time zone. The very same instant translates to the same day, but 8:46:20.191 in the GMT+01 time zone. If your input string specified "7:46:20.191" and you indeed got 1239867980191 from Date.getTime()then congratulations, the parsing code understood your "7:46:20.191" as to be interpreted in the GMT time zone, and did it properly.

自纪元以来的 1239867980191 毫秒转换为格林威治标准时间时区的 2009 年 4 月 16 日星期四 7:46:20.191。同一时刻转化为同一天,但在 GMT+01 时区是 8:46:20.191。如果您的输入字符串指定了“7:46:20.191”并且您确实从那时获得了 1239867980191,Date.getTime()那么恭喜您,解析代码将您的“7:46:20.191”理解为在 GMT 时区进行解释,并且正确执行。

If afterwardsyou get "8:46:20" when printing, this is only because you use the GMT+01 time zone for displaying that instant. Note that the string contains GMT+100precisely to notify you that it uses that time zone for display purposes. The instantwhich the Dateinstance represents is nonetheless exactly the instant you wish it to contain. Remember that a Dateinstance represents an instant in time, for which no notion of time zone applies: time zones are used to convert instants into calendar elements (days, hours...) and back.

如果之后打印时出现“8:46:20”,这只是因为您使用 GMT+01 时区来显示该时刻。请注意,该字符串包含GMT+100精确地通知您它使用该时区进行显示。该瞬间Date实例表示仍然是正是你希望它包含的瞬间。请记住,Date实例代表时间中的瞬间,对此不适用时区概念:时区用于将瞬间转换为日历元素(天、小时...)并返回。

To convert a Dateto a displayable string, use DateTimeFormat.format(Date, TimeZone)which lets you specify which time zone you want to use for that string.

要将 a 转换Date为可显示的字符串,请使用DateTimeFormat.format(Date, TimeZone)which 让您指定要用于该字符串的时区。

回答by Igor Klimer

Since the Calendarclass is not supported in GWT, maybe something hackish like this will work:

由于Calendar该类在 GWT 中不受支持,也许像这样的 hackish 会起作用:

final String timezone = "GMT-07:00";
DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ssZZZZ");
long unix = dtf.parse("2009-10-12T00:00:00" + timezone).getTime();

This way you can provide the correct timezone info - though, that should be the default behaviour.

这样您就可以提供正确的时区信息——不过,这应该是默认行为。

回答by Thomas Pornin

It is the other way round. A Dateinstance holds the time in milliseconds since the Epoch, using the UTC time scale (i.e. leap seconds are ignored). This is what Date.getTime()returns and that's what you want.

反之亦然。一个Date实例使用 UTC 时间刻度(即忽略闰秒)保存自纪元以来的时间(以毫秒为单位)。这就是Date.getTime()回报,这就是你想要的。

The culprit here is the parser, which interprets the date you give as a string in your local time zone. If you want DateTimeFormatto interpret the string as a date-and-time given in the UTC time zone, append an explicit time zone to the parsed string:

这里的罪魁祸首是解析器,它将您提供的日期解释为本地时区中的字符串。如果要将DateTimeFormat字符串解释为 UTC 时区中给定的日期和时间,请将显式时区附加到解析的字符串:

DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ssZZZZ")
    .parse("2009-10-12T00:00:00.000" + " GMT");

(The above assumes that I understood GWT documentation properly; I have not tried.)

(以上假设我正确理解 GWT 文档;我没有尝试过。)

Just to be clear in my notations: for all practical purposes, there is no difference between "GMT" and "UTC", and there is no daylight saving in the GMT time zone. Other time zones are often defined as "GMT plus or minus some offset" and the offset may change between summer and winter. For instance, the time zone in New York is somewhat equivalent to "GMT-04" in summer and "GMT-05" in winter.

只是在我的符号中要清楚:出于所有实际目的,“GMT”和“UTC”之间没有区别,并且 GMT 时区没有夏令时。其他时区通常定义为“GMT 加上或减去一些偏移量”,并且偏移量可能会在夏季和冬季之间发生变化。例如,纽约的时区在某种程度上相当于夏季的“GMT-04”和冬季的“GMT-05”。

回答by Powerlord

I keep seeing formats with ZZZZ being suggested... but why?

我一直看到建议使用 ZZZZ 格式...但为什么呢?

"yyyy-MM-dd'T'HH:mm:ss.SSSZ" would match

"yyyy-MM-dd'T'HH:mm:ss.SSSZ" 会匹配

"2009-10-12T00:00:00.000-0000"

“2009-10-12T00:00:00.000-0000”

The last part being the offset from UTC; California (to use someone else's example time) would be -0800, -0700 in summer.

最后一部分是与 UTC 的偏移量;加利福尼亚(以其他人的时间为例)夏季为 -0800、-0700。

As a side note, GMT is also always -0000. That's why Britain's summer time zone is BST (British Summer Time, +0100).

附带说明一下,GMT 也总是 -0000。这就是为什么英国的夏季时区是 BST(英国夏令时,+0100)。

回答by justkt

Try the Calendarobject.

试试Calendar对象。

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
Date dataObject = DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ss.SSS")
                  .parse("2009-10-12T00:00:00.000);
cal.setTime(dataObject);
cal.getTimeInMillis();

According to the API, getTimeInMillis() returns "the current time as UTC milliseconds from the epoch."

根据 API,getTimeInMillis() 返回“当前时间作为 UTC 毫秒从纪元开始”。

EDIT: as _bravado pointed out, the Calendar API is currently not available for GWT (Issue 603). While this would get the appropriate time in a Java application, it isn't going to work here. There is information in the groupabout using GMT.

编辑:正如 _bravado 指出的,日历 API 目前不适用于 GWT(问题 603)。虽然这将在 Java 应用程序中获得适当的时间,但它在这里不起作用。还有就是在信息有关使用GMT。

EDIT: Missing a closing bracket on the the Calendar.getInstance() call

编辑:缺少 Calendar.getInstance() 调用上的右括号