How to convert Client’s TimeZone to a Java Time Zone
Recently I had a client time zone issue with a web based calendaringĀ application.
I needed to get the clients TimeZone and alter the events listed in a calendar view to reflect the clients time zone.
Heres how I did it
On the Client side I used some javascript to get the clients offset to UTC in milliseconds.
<script type="text/javascript"> var MILISECS_PER_HOUR = 60 * 60 * 1000; var date = new Date(); var localOffset = (-(date.getTimezoneOffset()/60) * MILISECS_PER_HOUR); document.cookie = 'clientTimeZoneOffsetMs='+localOffset+'; path=/'; </script>
Then on the Java side I read the cookie and call the following function
public static TimeZone getTimeZoneFromClientOffset(String clientOffsetMs){
int intClientOffset = 0;
try{
intClientOffset = Integer.parseInt( clientOffsetMs );
return getTimeZoneFromClientOffset(intClientOffset);
}catch(Exception exc){
return null;
}
}
Enjoy
Advertisement
Leave a Comment