Get TimeZone by Country/Region in Java

2019-08-28 03:41发布

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.

标签: java timezone
4条回答
The star\"
2楼-- · 2019-08-28 04:19

Two approaches you can take:

  1. Detect platform settings
  2. 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.

查看更多
霸刀☆藐视天下
3楼-- · 2019-08-28 04:27

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.

查看更多
冷血范
4楼-- · 2019-08-28 04:39

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;
}
查看更多
我命由我不由天
5楼-- · 2019-08-28 04:45

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.

查看更多
登录 后发表回答