I have my application hosted in US. The server Time Zone is in GMT. Now when people accessed this application from across the Globe I want to get the Time zone for the Country/Regions from where it is accessed. Say - I'm accessing from some where in Europe. I want to get the time zone for that particular regions. I want this to be done in Java.
-
Is this a web application or something else? – Jon Skeet Jan 03 '13 at 08:36
-
i dont think the jdk/jre packs this. they have a TZ database, but they dont map location to TZ – radai Jan 03 '13 at 08:40
-
@Jon, This is a Web application – irengba Jan 03 '13 at 08:42
-
@user1445777: In which case you're relying on Javascript to give you the time zone really, unless you know where the user is in some other way. *Do* you know that? (IP geocoding is one option, for example - or the user may have specified their location in some other way.) – Jon Skeet Jan 03 '13 at 08:43
4 Answers
I'm not entirely sure what you're actualy asking as I believe you could answer this question yourself.
You cannot get a timezone from the http request directly as mentioned here:
Is there another way to get a user's time zone from a HttpServletRequest object in Spring MVC?
But you can determine it by ip address. Mentioned here:
How to programmatically find geo location of incoming http request?
You can also use getTimeZoneOffset(). Mentioned here:
User needs to display transaction in there local time as they login from different country.
Prompting user for gmt or country code (and calculating gmt from that) in the registration still seems easier.
You will need to map the clients IP address to a geo location first.
There are quite a few databases, that have this information.
For example, have a look at DB-IP, a database that has mapping information for IP addresses to various location information.
You can load the data to a local database and query it with the calles IP address. (You have this in your HttpRequest
, when you are using Java Servlets.)
The commercial version of the database ($90) has also the timezone in it.
Other possibilities include the use of a Web Service, like IP2Location. You would request the the data from the service, when a request is made by the client. Of course, this can be slow and you rely on the availability of the remote service.
There are other databases and services around, find the one that fits your needs.
I did not find one that is free and has the timezone information in it. A country information is surely not enough, when it comes to bigger countries.

- 5,150
- 1
- 28
- 41
Two approaches you can take:
- Detect platform settings
- IP geocoding
Approach 1 is to use javascript for this. I've used a module i found here with good results. I used code similar to this on a "jump page" that just picks up the timezone and then redirects on (passing the timezone as a parameter) in the login flow.
<script type="text/javascript" src="/script/detect_timezone.js"></script>
<script type="text/javascript">
var tz_info = jstz.determine_timezone();
var timezone = tz_info.timezone;
var tz_id='';
if (timezone != 'undefined') {
timezone.ambiguity_check();
tz_id = timezone.olson_tz;
alert(tz_id); // olson timezone, e.g. "Europe/London", "Asia/Tokyo" etc.
}
</script>
This is a lot easier than messing around with IP geolocation and since most people have their computer/device set to their "correct" timezone, it will be right most of the time.

- 27,064
- 6
- 41
- 46
First we have to find the Time zone of a client.
Then as per the timezone we have to fetch time as follows:
public String getLocalTime(String timezone) {
TimeZone tz = TimeZone.getTimeZone(timezone);
Calendar localtime = new GregorianCalendar(tz);
String strLocalTime = localtime.get(Calendar.HOUR_OF_DAY) + ":"
+ localtime.get(Calendar.MINUTE) + ":" + localtime.get(Calendar.SECOND);
return strLocalTime;
}

- 321,522
- 82
- 660
- 783