In my previous project (a web application used by clients from offices in different locales), we used to rely on getTimezoneOffset() value of the Date object in Javascript to find out the client's time zone offset, maintain a map of the offset to the country and display the zone appropriately.
E.g.
- 330 -> IST
- -240 -> EST
- -200 -> EST5EDT
etc.,
The timezoneoffset used to be stored in the cookie on the client side and used to come with every request on the server side. If the cookie is not found (happens when the user clears off cookies), we used have an intermediate page that determines the timezone offset, sets the cookie and redirects the user to the page he intended to visit.
Setting the cookie on the client side (sample code):
document.cookie=
"clientOffset=" + new Date()).getTimezoneOffset()
+ "; expires=" + expireDate.toGMTString()
+ "; domain=<your domain>;";
On the server side,
// aRequest is of type HttpServletRequest
Cookie [] cookies = aRequest.getCookies();
if (null == cookies) {
// redirect the user to the intermediate page that gets the client offset and
// takes the user the actually-intended page.
return;
}
for (Cookie cookie : cookies) {
// Find the cookie whose name matches the one you are looking for and
// read the value and parse to an integer.
}
The date is converted to the user's time zone as follows:
// Here 'date' represents the date to be displayed in the server's time zone.
Date date = new Date();
SimpleDateFormat userDateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss.SSS z");
// As mentioned above, you would maintain a map of clientOffset to timezone ID
// Let's say your client is in EST time zone which means you will get -240 as the
// client offset.
userDateFormat.setTimeZone(TimeZone.getTimeZone("EST"));
// This would convert the time in server's zone to EST.
System.out.println(userDateFormat.format(date));
Hope this helps!