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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

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

  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Accelerating Debugging in Integration Testing: An Efficient Search-Based Workflow for Impact Localization
  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  • Cloud Security and Privacy: Best Practices to Mitigate the Risks
  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.4K 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.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: