Details
-
Bug
-
Status: Closed
-
Critical
-
Resolution: Duplicate
-
5.1.0.5
-
None
Description
If the client is in a different timezone to the server, selecting a date will set the wrong value in the input field in most cases.
The cause is the use of 'milliseconds since epoch'.
The following demonstrates part of the problem:
TimeZone serverTz = TimeZone.getTimeZone("GMT+1000");
TimeZone clientTz = TimeZone.getTimeZone("GMT-1500");
// simulates client to server communication method used by DateField
TimeZone.setDefault(serverTz);
SimpleDateFormat serverDf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat clientDf = (SimpleDateFormat) serverDf.clone();
clientDf.setTimeZone(clientTz);
String dateSelection = "2011-07-01 00:00:00";
Date clientDate = clientDf.parse(dateSelection);
Date serverDate = new Date(clientDate.getTime());
System.out.println("2011-07-01 00:00:00 = " + serverDf.format(serverDate) + "?");
So if a user selected July 7 2011, the field would display July 2 2011 if the timezones were as above.
My solution was to remove the usage of both the java and javascript `Date.getTime` and `new Date(time)` functions. Instead, I serialise to "yyyy-MM-dd" and format/parse this on the server using the same tz as the format parameter.