Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Client side time zone support in GWT

Tags:

java

timezone

gwt

I'm working on a GWT app where I need to support the following scenario:
The server is located in time zone A
The client's browser is set to time zone B
The GWT app is configured to display date/time in time zone C

Since GWT does not support Calendar and the native support for time zones in javascript is non-existent I can't think of a nice and clean solution to this problem.

Have any of you done something similar or do you know of any good utils I could use?

Thanks!

like image 915
Hedenius Access Avatar asked Nov 06 '09 09:11

Hedenius Access


2 Answers

In my experience, the following best practice significantly reduces complexity and confusion when dealing with dates and timezones in gwt:

  1. 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.
  2. 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 your own date formatter util) to convert it into either timezone B or timezone C as appropriate.

like image 60
Upgradingdave Avatar answered Sep 18 '22 13:09

Upgradingdave


You can't 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:

  1. You manage the timezone conversion yourself.

  2. 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!.

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

like image 43
Fedearne Avatar answered Sep 18 '22 13:09

Fedearne