Mayank Pandya
Mayank Pandya

Reputation: 1623

GWT timezone issue

From few days I am fighting with different Timezone issues of server and GWT client. but cannot get any success.

Scenario is Server is in UTC timezone let say Client A is in IST timezone. When client select a date (with time) I pushed to server but date is automatically changed to server's timezone. I dig around this issue and I found multiple solutions like

  1. create custom serializer (No idea how to do that can't found any proper example)
  2. transfer date as a string to server and convert it to server timezone and store it. and when fetching data convert again from server's timezone to client local timezone. sounds good idea.

So my query is.

  1. Any other solutions?
  2. which is best way to manage this ?
  3. any sample code or link?

Upvotes: 1

Views: 4366

Answers (1)

Braj
Braj

Reputation: 46841

Simply format the date in UTC format at client side and pass the date as string to the server and store the data in UTC format in database as well.

On the server side everything would be fine, since formatting the date would use the timezone of the server, which is what the date is stored in. On the client side, however, GWT will use the timezone of the client machine, and so there could be a discrepancy.

Sample code:

DateTimeFormat f = DateTimeFormat.getFormat("MMM dd yyyy");
TimeZoneConstants t = (TimeZoneConstants) GWT.create(TimeZoneConstants.class)
TimeZone est = TimeZone.createTimeZone(t.americaNewYork());
int offset = est.isDaylightTime(date) ? +240 : +300;
TimeZone tz = TimeZone.createTimeZone(offset);
String date = f.format(user.getBirthDate(), est);

There are a few other possible solutions, but one of these two might do the trick.

Upvotes: 1

Related Questions