Skip to content

How to convert Client’s TimeZone to a Java Time Zone

January 23, 2011

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

From → Coding, websites

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.