tags:

views:

798

answers:

5

[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: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?

+1  A: 

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.

justkt
I don't think you can use Calendar with GWT?
day_trader
Very good point.
justkt
+2  A: 

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.

Igor Klimer
This is the kind of thing I was trying. However I couldn't get a right combination at all. I would have thought the time zone code would be "UTC" for UTC? Isn't -7:00 California or something?
day_trader
I added `"GMT-07:00"` as an example, you should change to whatever value is working for you ;] (so that you get your "UNIX Time milliseconds using a GMT with daylight savings" ;))
Igor Klimer
+1  A: 

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.

Thomas Pornin
I think GMT automatically includes daylight savings - which is what I do not want. I want plain UTC or normal GMT.
day_trader
Correct that - it does. :(
day_trader
@_bravado: It doesn't, hence why Britain uses BST in summer.
R. Bemrose
Well I don't know what your doing, but for me - I'm telling Java it needs to be GMT and it's making it Summertime. So I repeat, it **doesn't**, or maybe you or whoever else disagrees cares to explain their answer properly.
day_trader
A: 

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

R. Bemrose
I'm passing in a Timestamp extracted from a file that is in UTC. I'm passing it in with your suggestions - and it is converting it to GMT Summertime - yet again. Either you're not considering GWT Client-Side or something is fundamentally wrong with my GWT - which is the latest release.
day_trader
+2  A: 

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.

Thomas Pornin
+1 for nice clarification and pointing to `DateTimeFormat.format(Date, TimeZone)`
Igor Klimer
Wow, this has been one confusing situation. It seems you are correct. Thanks for the extra clarity :)
day_trader