在 Java 中按国家/地区获取时区

编程入门 行业动态 更新时间:2024-10-21 13:43:12
本文介绍了在 Java 中按国家/地区获取时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我的应用程序托管在美国.服务器时区采用 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:

检测平台设置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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 09:48:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1391998.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时区   地区   国家   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!