How do I get GWT DateTimeFormat to display using the server TimeZone, rather than the client's?
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.
How do I get the Formatter (DateTimeFormat) to display the date using the server's timezone r开发者_运维问答ather than the user?
Thanks
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());
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).
Will the date be modified on the client? If not do the format on the server and just send over a string value. One last thing. There seems to be some issues with Dates on the client side in GWT. Refer to http://blog.gerardin.info/archives/674
精彩评论