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

10,335

Solution 1

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

Solution 2

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

Share:
10,335
RodeoClown
Author by

RodeoClown

I filled 'About Me' out purely to get a badge!

Updated on June 09, 2022

Comments

  • RodeoClown
    RodeoClown almost 2 years

    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