0

If my java web application is deployed in the US I will get US time regardless of the client's country. But if the client is in India, he should get Indian time, either using the time zone or locale or user preference. What should I do?

Can anyone help me please.

Josuvam
  • 9
  • 7

2 Answers2

1

Determine country by user IP address (using some web service or any special local library). And display the time according to appropriate time zone if it's India.

kapandron
  • 3,546
  • 2
  • 25
  • 38
  • I think it's not possible because it's web application where user login from different country through internet – Josuvam Aug 27 '12 at 14:26
  • Do you mean that the user is from India but not physically located in India and can be in another country in the moment? This case are you considering? – kapandron Aug 27 '12 at 14:33
  • In this case you have to get user's local with a JavaScript. – kapandron Aug 27 '12 at 14:35
  • Thanks .Yes your correct ,Any code related to this your having .Can you please help me out. Thanks in advance – Josuvam Aug 27 '12 at 14:40
  • Thanks .Let me try this . I need to set the following code in javascript while displaying the US date so that i will change according to the local time . – Josuvam Aug 27 '12 at 15:19
  • can you please provide any link It would be easy for me . Sorry for disturbing you again – Josuvam Aug 28 '12 at 13:05
  • Show me how you get and display a date to user. – kapandron Aug 28 '12 at 13:13
  • One of the most appropriate ways to address I see the following. You can get user timezone and convert it to the user's local timezone on the client side. Use `new Date().getTimezoneOffset()/60` to getting timezone. There's also `toLocaleString()` method to displaying date according locale. See [example](http://www.w3schools.com/jsref/jsref_gettimezoneoffset.asp). – kapandron Aug 28 '12 at 13:14
0

In my previous project (a web application used by clients from offices in different locales), we used to rely on getTimezoneOffset() value of the Date object in Javascript to find out the client's time zone offset, maintain a map of the offset to the country and display the zone appropriately.

E.g.

  • 330 -> IST
  • -240 -> EST
  • -200 -> EST5EDT

etc.,

The timezoneoffset used to be stored in the cookie on the client side and used to come with every request on the server side. If the cookie is not found (happens when the user clears off cookies), we used have an intermediate page that determines the timezone offset, sets the cookie and redirects the user to the page he intended to visit.

Setting the cookie on the client side (sample code):

document.cookie= 
    "clientOffset=" + new Date()).getTimezoneOffset() 
    + "; expires=" + expireDate.toGMTString() 
    + "; domain=<your domain>;";

On the server side,

// aRequest is of type HttpServletRequest
Cookie [] cookies = aRequest.getCookies();
if (null == cookies) {
    // redirect the user to the intermediate page that gets the client offset and 
    // takes the user the actually-intended page. 
    return;
}
for (Cookie cookie : cookies) {
    // Find the cookie whose name matches the one you are looking for and 
    // read the value and parse to an integer.
}

The date is converted to the user's time zone as follows:

// Here 'date' represents the date to be displayed in the server's time zone.
Date date = new Date();
SimpleDateFormat userDateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss.SSS z");
// As mentioned above, you would maintain a map of clientOffset to timezone ID
// Let's say your client is in EST time zone which means you will get -240 as the
// client offset.
userDateFormat.setTimeZone(TimeZone.getTimeZone("EST"));

// This would convert the time in server's zone to EST.
System.out.println(userDateFormat.format(date));

Hope this helps!

Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • Thanks for your reply . Any sample code related to this requirement so that it would be helpful for me. Thanks in advance – Josuvam Aug 27 '12 at 14:22
  • Thanks for your reply . First thing what I need to do set the cookies from the client side(Browser) using the Java script code which you given . Second Step I need to read the cookies from the server Side get the timeZoneOffset parameter.with the help of this parameter I need to change the US time which I have to local time . IF I am wrong anywhere .Please correct me . Thanks in advance – Josuvam Aug 27 '12 at 14:55
  • From clientSide I have got the value as -330 .How I need to manipulate the offset with the date what I have .Can you please help me with sample code. – Josuvam Aug 28 '12 at 13:03
  • Please see the last section in my response edited just now, to include the code snippet to convert date from your server's timezone to user's timezone. – Vikdor Aug 28 '12 at 13:31