Reputation: 1623
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
So my query is.
Upvotes: 1
Views: 4366
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