DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Adding Two Hours in DataWeave: Mule 4
  • Protect Your Invariants!
  • Leveraging Salesforce Using a Client Written In Vue.js
  • External Task Client Implementation in Camunda With Spring Boot Application

Trending

  • Event-Driven Pipelines With Apache Pulsar and Go
  • Pragmatica Aether: Let Java Be Java
  • Jakarta EE 12: Entering the Data Age of Enterprise Java
  • Liquid Glass, Material 3, and a Lot of Plumbing
  1. DZone
  2. Culture and Methodologies
  3. Agile
  4. Get Client (Browser) timezone and maintain it in cookie

Get Client (Browser) timezone and maintain it in cookie

By 
Rajeshkumar Dave user avatar
Rajeshkumar Dave
·
Mar. 28, 15 · Interview
Likes (0)
Comment
Save
Tweet
Share
12.8K Views

Join the DZone community and get the full member experience.

Join For Free

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.

application Spring Framework Requirement Pass (software) JavaScript Convert (command) Implementation

Opinions expressed by DZone contributors are their own.

Related

  • Adding Two Hours in DataWeave: Mule 4
  • Protect Your Invariants!
  • Leveraging Salesforce Using a Client Written In Vue.js
  • External Task Client Implementation in Camunda With Spring Boot Application

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook