Java Server and Client Time Zone Difference Problem – Solved

In this post we are going to see the difference between the server and client time zone and how to handle those in our applications i.e server and client time zone difference ??.

Today almost each and every application i.e from standalone scale to enterprise scale deal with Date and Time in their process flow. Whenever client (application user) performs any action in the application we always capture those actions and the date time data of that moment  e.g booking an online ticket , making an online payment etc. Now it is quite possible that your client and server are present at different locations i.e different timezones, generally servers are present at a particular location and client across the globe. So, let’s first look at the definition of server time and client time.
Server Time : In application terminology ,server time is the time of that locale(timezone) where that server is located . For example if my server is located in India then it will come under the IST(Indian Standard Time) timezone.

Client Time : In the same context , client time refers to the timezone of the locale from where my client is accessing the application. For example if my client is accessing from Los Angeles USA, then their timezone will be Pacific Time Zone.

Problem Statement : Here we will assume that my server is present in India and user is accessing the application from USA.
As an user of the application I always want to see the time that belongs to my timezone across the application i.e USA timezone. For example , if I am booking a flight I would like to see the timings for flight as per my timezone not as per  the timezone of server. If your client and server is in the same timezone then you might not face any problem but believe me most of the time it is not going to happen because most of time whenever any web application is launched , it will be open for all the users across the globe except few cases.
Real problem will begin when your application is showing the server time(India Time) to the client(who is in USA). Now we will see how we are going to tackle this problem , I will provide you 2 solutions , in which one of I have already tried and tested and 2nd one I am working on .

Assumption : All the date and time that you are storing in your database is following the GMT Timezone that is also know as UTC (Universal Coordinated Time). In my application every date and time instance is stored following the same timezone i.e UTC.

Solution 1 : Before we start looking into the solution , there is something that you should know about the Date and Time API in java .

Date value doesn't have a time zone. It just represents an instant in time.
When you call Date.toString(), that uses the default time zone of the
system you're calling it on. So , be careful when you use these APIs for Date 
and Time conversion or for sending Date and Time data to the server.

In solution 1 ,we have to write some code on both client and server side , so that after getting the data (date and time from the server) it will show the data in the same timezone in which client is .
• Client Side : For this you have to use the Date class present in the javascript library. If you look into this class , you will find a method getTimezoneOffset() that gives the timezone of the client actually it gives the difference of client timezone and GMT . e.g :for a user who is in India it will give -330 (minutes) .
Note : IST is 5:30 hours ahead of GMT.

For this you have to write some code more specifically javascript that will get executed (on page load) when you receive the first request for your application and will fetch the timezone of the client and store it in cookie and send it to server. Below is the sample code for doing same using cookies:

<script type="text/javascript">
    function setTimeZoneInCookie() {
        var _myDate = new Date();
        var _offset = _myDate.getTimezoneOffset();
        document.cookie = "TIMEZONE_COOKIE=" + _offset; //Cookie name with value

Important Note : You can’t set this value in session because session is a server side property and it is not accessible from client code.

Java Server Vs Client Zone Difference Problem

Server Side : On the server side you don’t have to do much , just make sure that after fetching the data (Time and Date) from the server in UTC format , convert it back according to the user timezone with the help of value that you received in cookie.
On server side you can have one utility function like this that will give you back the cookie value.

public String getTimeOffset(HTTPServletRequest request){
Cookie[] cookies = servletRequest.getCookies();
String timeOffset=null;
if (cookies != null) {
 for (Cookie cookie : cookies) {
   if (cookie.getName().equals("TIMEZONE_COOKIE")) {
  return timeOffset;

//Add the offset to the time that you got from server , you have to write the code something like this
Calendar now = Calendar.getInstance(); // in your case now will be the server time after getting from DB
now.add(Calendar.MINUTE, offset*(-1));

So, here we are done with our first solution using javascript Date API. I have tested this solution and it worked like charm for me. I believe that it will work for you also.

Solution 2 : I am not sure how Joda Date Time API will solve this problem but this API is very powerful and can solve any type of Date and Time problem very efficiently. These days I am busy with experimenting with this new API and will post my findings very soon.

From the Documentation:
“Joda-Time has been created to radically change date and time handling in Java. The JDK classes Date and Calendar are very badly designed, have had numerous bugs and have odd performance effects.”

Please share your thoughts and comments on the same. If you have any other alternative solution available please share it.

Image Source :


Saurabh Jain

A Developer working on Enterprise applications ,Distributed Systems, Hadoop and BigData.This blog is about my experience working mostly on Java technologies ,NoSQL ,git , maven and Hadoop ecosystem.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

2 thoughts on “Java Server and Client Time Zone Difference Problem – Solved

  1. Pingback: Free Content For Your Blog
Add Comment Register

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>