Client side time zone support in GWT?

In my experience, the following best practice significantly reduces complexity and confusion when dealing with dates and timezones in gwt: Whenever operating/storing dates within the application, treat all dates as milliseconds since epoch in GMT timezone. You can store them as string or int, it doesn't really make a difference Whenever displaying the date to the end user, format the date using appropriate timezone For your case, when you create a date on the Server (timezone A) convert it to milliseconds since epoch in GMT before sending it to the Client. On the client, use DateTimeFormat (or write you're own date formatter util) to convert it into either timezone B or timezone C as appropriate.

In my experience, the following best practice significantly reduces complexity and confusion when dealing with dates and timezones in gwt: Whenever operating/storing dates within the application, treat all dates as milliseconds since epoch in GMT timezone. You can store them as string or int, it doesn't really make a difference. Whenever displaying the date to the end user, format the date using appropriate timezone.

For your case, when you create a date on the Server (timezone A) convert it to milliseconds since epoch in GMT before sending it to the Client. On the client, use DateTimeFormat (or write you're own date formatter util) to convert it into either timezone B or timezone C as appropriate.

I missed the section on handling time zones in the DateTimeFormat java doc. Seems it provides exactly the kind of fuctionality I need! I will use it in combination with the approach you propose.

Thanks! – sarnelid Nov 8 '09 at 15:12.

You cat change the GWT timezone, hence all java.util. Date's has the browser timezone. You will need to handle the current timezone setting manually.

I see 3 options: You manage the timezone conversion yourself. You override the serializer/deserializer of java.util. Date like in this post.

And maybe using a custom java.util. Date implemtation, that overrides the getTimezoneOffset(). This approach requires recompilation of the GWT API!.

You implement your own Date, either by extending java.util. Date (like in option 2) or wrapping it with some timezone object. In this option CustomFieldSerializer's may still be usefull, but there is no need for recompiling the GWT API.

I would prefer option 3. At least until GWT RPC maybe someday will support for overriding the CustomFieldSerializer's Usefull date/time formatting hints.

I'm assuming you are using RPC calls for server-client communication here. Also assuming that you don't care about timezone B, and you know what timezone C is on the server. You have a few options here: Calculate the desired date in the server (no Java limits on what you can do there) and send it in a String to be displayed to the client, so you don't have to do anymore transformations on the client.

Or: Calculate the offset between timezone A and C on the server, apply it to all the Date objects you are passing to the client and just display them on the client. If for some reason none of these were valid for you Calculate the offset, send it to the client and apply it to any Date you receive from the server by transforming to ms, adding the offset and then creating a Date object again.

See this demo project GWT timezone demo project.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions