开发者

Time Zones in Java / GWT (Client-side)

开发者 https://www.devze.com 2022-12-18 04:35 出处:网络
[Client-side GWT class] I have a Date Object... Date dataObject = DateTimeFormat.getFormat(\"yyyy-MM-dd\'T\'HH:mm:ss.SSS\")

[Client-side GWT class]

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?


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

'Thu Apr 16 08:46开发者_StackOverflow社区: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!).

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?


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.

If afterwards you 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+100 precisely to notify you that it uses that time zone for display purposes. The instant which the Date instance represents is nonetheless exactly the instant you wish it to contain. Remember that a Date instance 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.

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


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

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.


It is the other way round. A Date instance 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.

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

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.)

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.


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

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

"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.

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


Try the Calendar object.

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."

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 group about using GMT.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消