We store our dates are stored in milliseconds since the epoch and the Olson time zone ID for the objects we want to display time related data about.
How can I convert the Olson TZID to a TimeZoneConstant to create a TimeZone and use DateTimeFormat?
// values from database
String tzid = "America/Vancouver";
long date = 1310771967000L;
final TimeZoneConstants tzc = GWT.create(TimeZoneConstants.class);
String tzInfoJSON = MAGIC_FUNCTION(tzid, tzc);
TimeZone tz = TimeZone.createTimeZone(TimeZoneInfo.buildTimeZoneData(tzInfoJSON));
String toDisplay = DateTimeFormat.getFormat("y/M/d h:m:s a v").format(new Date(date), tz);
Does that MAGIC_FUNCTION exist? Or is there another way to do this?
Doing a GWT.create on the TimeZoneConstants class is bad game according to the GWT Javadoc [1]. So what I did was create a class on the server-side that parses /com/google/gwt/i18n/client/constants/TimeZoneConstants.properties and builds a cache of all the JSON objects for each time zone (indexed by their Olson TZID).
My site is running on jboss, so I copied TimeZoneConstants.properties into my site’s war/WEB-INF/lib directory (probably didn’t need to copy it there since the GWT jars are already there). Then I have a singleton class that does the parsing when constructed:
Finally I make an RPC call to get the TimeZoneInfoJSON to the client (assuming the server knows which TimeZoneID I’m interested in):
Not the most elegant solution but it has given me a way to display dates and times for a specific time zone over DST transitions.
[1] http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/i18n/client/constants/TimeZoneConstants.html