获取基于Java中UTC偏移量的时区快捷方式 [英] Get timezone shortcut based on UTC offset in Java

查看:212
本文介绍了获取基于Java中UTC偏移量的时区快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于UTC偏移量获取时区快捷方式,例如EST(东部标准),PST(太平洋)等。我知道这不是一个简单的问题,并且基于特定的偏移量可以有多个位置,但是可以。



我正在尝试使用Util Calendar对象来获取它,但是我似乎没有得到一个字符串,而只是得到了偏移量。

  public字符串foo(int offset)
{
....
返回TimeZoneShortcut ;
}

预先感谢。

解决方案

用户2580516的答案是正确的。我可以再添加一点。



避免使用三个字母的代码



三个字母的时区ID为既不规范也不独特。避免他们。



例如, IST 表示印度标准时间 爱尔兰标准时间。这样的冲突很多。



时区名称



请使用正确的时区,而不要使用3个字母的代码名称。例如:欧洲/巴黎,美国/蒙特利尔和亚洲/加尔各答。



似乎没有时区名称的官方标准。这让我感到惊讶;希望我错了,有人可以填补我的麻烦。无论如何,通常使用的列表来自 tz数据库 (以前称为 Olson数据库),称为

出色的日期时间库 Joda-Time ,提供了一种生成其当前已知的时区名称



时区名称会随着时间的变化而变化,其中一些会被添加,它们的规则也会随之变化。所有这些都是由政客和官僚决定的,因此变革是在最后时刻进行的,并不总是明智的。因此,您应该注意保持日期时间库为最新,或者至少更新其包含的时区数据库。



不可能的问题–无法确定时区



一个时区更多而不是与 UTC / GMT的数值偏移。时区还包含用于夏令时(DST)和其他异常的规则集。



因此,您无法从偏移量推断出时区。您可以猜测,但不能确定。



例如,取偏移量 +01:00 。是欧洲/巴黎还是非洲/拉各斯?两者都比UTC提前一小时。那么使用哪个有关系吗?是的…法国遵守夏令时,但尼日利亚没有。分配错误的时区意味着您的日期时间计算将是错误的。



另一种方式……也许是 +01:00 夏季在伦敦录制。在夏季,伦敦会观察夏令时并将时钟提前1小时。虽然标准时间是 +00:00 (在UTC / GMT上),但DST会将其提前了一个小时。



又一曲……即使您说随便挑一个,哪一个呢?对于仅在标准时间内的 +00:00 ,至少有2个三字母代码( CET MET )和37个跨越两个大洲的时区。



也许您在想,我可以用日期来计算如果DST生效,则退出。不会,DST的开始和结束时间在共享相同偏移量的各个时区中的不同日期。此外,某些国家(时区)足够明智,不会愚弄DST。



因此,关于您的问题不是一个简单的问题……但这没关系是错误的。这不是问题,这是不可能的。就像问题给生日,确定一个人一样。您可以确定某个人或时区不正确,但是您不能确定哪个正确是正确的。



记录带时间的时区



如果知道时区(它的位置和规则)对您很重要,则必须记录时区信息以及日期时间。例如,这可能意味着数据库中有一个额外的字段。



Java 8 带来了新的 java.time.8程序包,受 Joda-Time 的启发,由 JSR 310 定义。设计人员已经意识到时区作为日期时间值一部分的重要性。结果,他们的设计包括:




  • 主日期时间类以 Zoned开头,以强调该类包括时间区域信息: ZonedDateTime
  • $ b在ZonedDateTime类上的$ b
  • toString 实现扩展了 ISO 8601 格式,方法是将时区名称放在方括号中。代替:
    2014-02-14T20:51:55.427-08:00
    它输出
    2014-02- 14T20:51:55.427-08:00 [美国/洛杉矶]


I want to get the timezone shortcut like EST (for eastern standard), PST (pacific), and so on based on the UTC offset. I realize it's not a simple problem and there can be more than one location based on a particular offset, but that's okay.

I'm trying to get it using Util Calendar object but I don't seem to get a string but rather just the offset.

public String foo(int offset)
{
....
return TimeZoneShortcut;
} 

Thanks in advance.

解决方案

The answer by user2580516 is correct. I can add a bit more.

Avoid Three-Letter Codes

The three-letter time zone IDs are neither standardized nor unique. Avoid them.

For example, IST is used to mean India Standard Time or Irish Standard Time. There are many such collisions.

Time Zone Names

Instead of 3-letter codes, use proper time zone names. Examples: "Europe/Paris", "America/Montreal", and "Asia/Kolkata".

There does not seem to be an official standard for time zone names. That surprises me; hopefully I'm wrong and someone can fill me in. At any rate, a commonly used list is take from the tz database (formerly known as the Olson database), as listed in this Wikipedia page.

The excellent date-time library, Joda-Time, has a method to generate a list of its currently known time zone names.

The time zone names change over time, some are added, and their rules change too. All that is determined by politicians and bureaucrats, so changes are last-minute and not always sensible. So you should take care to keep your date-time library up-to-date, or at least update its contained time zone database.

Impossible Question – Cannot Determine Time Zone

A time zone is more than just an numerical offset from UTC/GMT. A time zone also contains the set of rules for Daylight Saving Time (DST) and other anomalies.

So you cannot infer a time zone from an offset. You can guess, but you cannot be sure.

For example, take the offset of +01:00. Is that "Europe/Paris" or "Africa/Lagos"? Both have an offset of one hour ahead of UTC. So does it matter which you use? Yes… France observes Daylight Saving Time but Nigeria does not. Assigning the wrong time zone means your date-time calculations will be wrong.

Another twist… Perhaps that +01:00 was recorded in London during the summer time. In summer, London observes DST and moves its clocks 1 hour ahead. While standard time there is +00:00 (on UTC/GMT), DST moves them one hour ahead of that.

Yet another twist… Even if you say "just pick one", which one? For +00:00 in just standard time, there are at least 2 three-letter codes (CET and MET) and 37 named time zones crossing two continents.

Perhaps you are thinking, "I can use the date to figure out if DST was in effect". Nope, DST starts and ends on different dates in various time zones sharing the same offset. Furthermore, some countries (time zones) are sensible enough to not fool with DST.

So regarding your question being "not a simple problem … but that's okay" is wrong. It's not a problem, it's impossible. Like the question, "Given a birthday, determine an individual person". You can determine that a person or time zone is not correct, but you cannot determine which is correct.

Record Time Zone With Time

If knowing the time zone (its locality and rules) is important to you, you must record the zone information along with the date-time. This may mean an extra field in your database for example.

Java 8 brings a new java.time.8 package, inspired by Joda-Time, defined by JSR 310. The designers have come to realize the importance of the time zone as a part of a date-time value. As a result, their designs include:

  • The main date-time class starts with the word "Zoned" to stress that the class includes time zone info: ZonedDateTime
  • Their toString implementation on the ZonedDateTime class extends the ISO 8601 format by appending the name of the time zone in brackets. Instead of:
    2014-02-14T20:51:55.427-08:00
    it outputs
    2014-02-14T20:51:55.427-08:00[America/Los_Angeles]

这篇关于获取基于Java中UTC偏移量的时区快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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