基于Java中的UTC偏移获取时区快捷方式

2022-01-11 00:00:00 date timezone calendar java

我想根据 UTC 偏移量获取 EST(东部标准)、PST(太平洋)等时区快捷方式.我意识到这不是一个简单的问题,基于特定偏移量的位置可能不止一个,但这没关系.

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.

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

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;
} 

提前致谢.

推荐答案

user2580516的回答是正确的.我可以再补充一点.

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

三个字母的时区 ID 既不标准化也不唯一.避开他们.

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

例如,IST 用于表示印度标准时间爱尔兰标准时间.有很多这样的碰撞.

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

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

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

似乎没有时区名称的官方标准.这让我感到惊讶;希望我错了,有人可以填写.无论如何,常用​​的列表来自 tz 数据库(以前称为 Olson 数据库),如 在此维基百科页面中列出.

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.

优秀的日期时间库,Joda-Time,有生成方法当前已知时区名称的列表.

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.

时区不仅仅是与 UTC/GMT.时区还包含 夏令时 (DST) 和其他异常情况的规则集.

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.

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

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.

另一个转折……也许那个 +01:00 是在夏季的伦敦录制的.在夏季,伦敦遵守 DST 并将时钟提前 1 小时.虽然标准时间是 +00:00(在 UTC/GMT 上),但 DST 将它们提前一小时.

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.

又一个转折点……即使你说随便挑一个",是哪一个?对于标准时间的+00:00,至少有2个三字母代码(CETMET)和37个命名时区跨越两大洲.

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.

也许您在想,我可以使用日期来确定 DST 是否生效".不,DST 在不同时区的不同日期开始和结束,共享相同的偏移量.此外,一些国家(时区)足够明智,不会被 DST 愚弄.

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.

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

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 带来了新的 java.time.8 包,灵感来自 Joda-Time,由 JSR 310.设计者已经意识到时区作为日期时间值的一部分的重要性.因此,他们的设计包括:

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:

  • 主要的日期时间类以Zoned"一词开头,以强调该类包含时区信息:ZonedDateTime
  • 他们在 ZonedDateTime 类上的 toString 实现扩展了 ISO 8601 通过在括号中附加时区名称来格式化.而不是:
    2014-02-14T20:51:55.427-08:00
    它输出
    2014-02-14T20:51:55.427-08:00[美国/洛杉矶]
  • 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]

相关文章