Over a million developers have joined DZone.

Get Client (Browser) timezone and maintain it in cookie

DZone's Guide to

Get Client (Browser) timezone and maintain it in cookie

· ·
Free Resource

Recently, I came with requirement where we need to get browser timezone and maintain it so our Spring MVC application can use it. Our application need to convert date and time from server timezone to client timezone.

Below is overall idea of implementation:

  1. Get Browser timezone by javascript. We can use opensource 'jstz.min.js' file for getting this. We can find this from ‘http://pellepim.bitbucket.org/jstz/’.
  2. We need to maintain this timezone. For same, we will store this timezone in cookie. This can be done by creating one jsp 'findTimeZonePage.jsp'. This page will store timezone in cookie and again redirect to original page. 
  3. Every method of Spring MVC controller will check whether cookie is available, If not then it will redirect to findTimeZonePage.jsp. While doing this we will also pass current Url(will set in model) so that findTimeZonePage jsp can redirect to same page again.


1. findTimeZonePage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<script src="/<WebContextPath>/js/jstz.min.js"></script>
<script type="text/javascript">
function timeFunction(){
var timezone = jstz.determine();
var timezoneName = timezone.name(); 
document.cookie="CalenderAppTimeZone=" + timezoneName + ";path=/";
alert("have set cookie. Redirecting to original page.");
var redirectUrl ="${redirectUrl}";



<body>loading the page...

2. Add below Methods in Util class:

public static TimeZonegetBrowserTimeZone(HttpServletRequest request){

  Cookie[] cookieArray = request.getCookies();

  if(cookieArray != null){

  for(Cookie cookie  : cookieArray){


  String timeZoneId = cookie.getValue();

  return TimeZone.getTimeZone(timeZoneId);




  return null;



   public static StringgetFullURL(HttpServletRequest request) {

   StringBuffer requestURL = request.getRequestURL();

   String queryString = request.getQueryString();

   if (queryString == null) {

       return requestURL.toString();

   } else {

       return requestURL.append('?').append(queryString).toString();



3. In each method of MVC Controller class, Add below code at start of method:

  TimeZone currentTimeZone = MyUtil.getBrowserTimeZone(request);

  if(currentTimeZone == null){

  String url = MyUtil.getFullURL(request);


  model.addAttribute("redirectUrl", url);

  //Redirect to 'findTimeZone' for setting timezone.

  System.out.println("####Timezone is not set. Redirecting to findTimeZone.jsp for setting timezone.");

  return "findTimeZonePage";


  System.out.println("####Current TimeZone="+currentTimeZone.getID());

Hope this will help.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}