在 Java 中按国家/地区获取时区 [英] Get TimeZone by Country/Region in Java

查看:69
本文介绍了在 Java 中按国家/地区获取时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序托管在美国.服务器时区采用 GMT.现在,当人们从全球范围内访问此应用程序时,我想从访问它的地方获取国家/地区的时区.说 - 我从欧洲的某个地方访问.我想获取该特定地区的时区.我希望这在 Java 中完成.

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.

推荐答案

您可以采取的两种方法:

Two approaches you can take:

  1. 检测平台设置
  2. IP 地理编码

方法 1 是为此使用 javascript.我使用了一个模块,我在 here 中找到了很好的结果.我在跳转页面"上使用了与此类似的代码,该代码仅获取时区,然后在登录流程中重定向(将时区作为参数传递).

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>

这比搞乱 IP 地理定位要容易得多,而且由于大多数人将他们的计算机/设备设置为他们的正确"时区,因此大部分时间都是正确的.

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.

这篇关于在 Java 中按国家/地区获取时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆