如何让GWT DateTimeFormat显示使用服务器TimeZone而不是客户端? [英] How do I get GWT DateTimeFormat to display using the server TimeZone, rather than the client's?
问题描述
我正在尝试显示一个(java.util。)日期客户端,并且不断使用浏览器的时区,导致不同的日期可见,具体取决于您查看页面的位置。
I am trying to display a (java.util.)Date client-side, and it keeps using the browser's timezone, resulting in a different date visible depending on where you view the page.
如何让Formatter(DateTimeFormat)使用服务器的时区而不是用户显示日期?
How do I get the Formatter (DateTimeFormat) to display the date using the server's timezone rather than the user?
谢谢
推荐答案
最简单的解决方案(不需要与服务器进行任何通信)只是强制 DateTimeFormat
使用特定的时区(您服务器所在的时区),如下所示:
The "easiest" solution (one that doesn't require any communication with the server) is just forcing DateTimeFormat
to use a particular timezone (the one your server is in), like this:
String pseudoServerTime = DateTimeFormat.getFullTimeFormat().format(new Date(), TimeZone.createTimeZone(TimeZoneConstants.europeWarsaw());
您可以将时区字符串/对象硬编码为 publ如果您移动/更改服务器(并且GWT编译器将内联这样做,因此没有性能损失),则可以轻松更改ic static final
。
You can hardcode the timezone string/object somewhere as public static final
so that it can be easily changed, if you move/change servers (and the GWT compiler will inline this, so no performance penalty).
这篇关于如何让GWT DateTimeFormat显示使用服务器TimeZone而不是客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!