Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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.

Code:

1. findTimeZonePage.jsp

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

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

timeFunction();

</script>

</head>
<body>loading the page...
</body>
</html>

2. Add below Methods in Util class:

public static TimeZonegetBrowserTimeZone(HttpServletRequest request){

  Cookie[] cookieArray = request.getCookies();

  if(cookieArray != null){

  for(Cookie cookie  : cookieArray){

  if("CalenderAppTimeZone".equals(cookie.getName())){

  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);

  System.out.println("Url="+url);

  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.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}