views:

537

answers:

1

I'm using com.google.gwt.i18n.client.timezone to try and display a date (as at the server), but GWT automatically adds the current timezone to the date when formatting it, meaning The wrong date is shown in different timezones.

To combat this, I'm sending the server's timezone offset to the client and using that when formatting.

I live in Australia and the current timezone is +11 GMT/UTC, but the default timezone being displayed when I format the date is -11 GMT.

The offset from the server is +11 hours (as it should be), but when I try and format the date with this offset, I get the wrong date, and so I need to use the negative offset instead.

Why is the default timezone wrong?

+1  A: 

When you are getting a date (particularly if you're parsing a date) make sure you specify the timezone. GWT's DateTimeFormat.parse only supports "RFC format" timezones, something like -0800 for Pacific time. If your server is sending dates in strings to the client, make sure it includes the timezone in this format.

Then when you convert the date to a string to present it to the user, make sure you use the overload of DateTimeFormat.format that specifies a TimeZone and pass the timezone that you want the date to be presented in (the timezone of the server, in your case.)

By default dates are presented in the timezone that the user's system is set to. Setting the default timezone in GWT (so you can ignore timezones and do everything in the server's timezone) is an open issue (3489) at the time I write this.

Dominic Cooney
Thanks Dominic, I am using the format method with a Date object from the server (not a String) and the timezone that I've also passed from the server. There's no parsing happening at all.
RodeoClown
Can you inspect the data in the Date object you're receiving from the server? What's in there? Also, just to sanity check, what is the timezone on the system that you're using the browser on?
Dominic Cooney
The timezone offset on the date is -660 minutes (which is using the TimeZone of the client). When I get the offset from the server it is 660 minutes. Fun :)
RodeoClown
It looks like the server is sending a date, and that is getting stored as a UTC date (hence the -660 offset on the client.) What timezone is the server in? And just to confirm: that's the timezone that you want to present dates in?
Dominic Cooney
Server is in Australian EST, meaning +11 at the moment, and +10 when daylight savings ends. And yes, it is the timezone I want to display dates in. (BTW - I will upvote later, just leaving it so the question is listed as unanswered for the time being)
RodeoClown