GWT时区问题 [英] GWT timezone issue

查看:134
本文介绍了GWT时区问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天后,我与服务器和GWT客户端的不同时区问题进行了对抗。但无法取得任何成功。

场景是服务器在UTC时区,假设客户端A位于IST时区。
当客户端选择日期(有时间)时,我推送到服务器,但日期自动更改为服务器的时区。我深入了解这个问题,并且发现了多个解决方案,如
$ b


  1. 创建自定义序列化程序(不知道该怎么做,找不到任何适当的示例)

  2. 将日期作为字符串传输到服务器,并将其转换为服务器时区并存储它。并在获取数据时再次从服务器的时区转换到客户端本地时区。听起来不错的主意。

所以我的查询就是。


  1. 任何其他解决方案?

  2. 这是管理此问题的最佳方法?

  3. 任何示例代码或链接? $ b


解决方案

在客户端简单格式化UTC格式的日期并将日期作为字符串传递给服务器并存储数据以数据库中的UTC格式。



在服务器端,一切都会好起来的,因为格式化日期将使用服务器的时区,这是日期然而,在客户端,GWT将使用客户端机器的时区,因此可能存在差异。



示例代码:

  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);






还有其他一些可能的解决方案,这两个可能会诀窍。




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?

解决方案

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.

这篇关于GWT时区问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆