Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get GWT DateTimeFormat to display using the server TimeZone, rather than the client's?

Tags:

date

timezone

gwt

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 rather than the user?

Thanks

like image 565
RodeoClown Avatar asked Feb 03 '10 01:02

RodeoClown


2 Answers

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

like image 172
Igor Klimer Avatar answered Oct 31 '22 04:10

Igor Klimer


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

like image 21
Carnell Avatar answered Oct 31 '22 03:10

Carnell