問題描述
我想根據(jù) UTC 偏移量獲取 EST(東部標(biāo)準(zhǔn))、PST(太平洋)等時(shí)區(qū)快捷方式.我意識(shí)到這不是一個(gè)簡(jiǎn)單的問題,基于特定偏移量的位置可能不止一個(gè),但這沒關(guān)系.
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 對(duì)象來獲取它,但我似乎沒有得到字符串,而只是得到了偏移量.
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的回答是正確的.我可以再補(bǔ)充一點(diǎn).
The answer by user2580516 is correct. I can add a bit more.
三個(gè)字母的時(shí)區(qū) ID 既不標(biāo)準(zhǔn)化也不唯一.避開他們.
The three-letter time zone IDs are neither standardized nor unique. Avoid them.
例如,IST
用于表示印度標(biāo)準(zhǔn)時(shí)間
或 愛爾蘭標(biāo)準(zhǔn)時(shí)間
.有很多這樣的碰撞.
For example, IST
is used to mean India Standard Time
or Irish Standard Time
. There are many such collisions.
不要使用 3 個(gè)字母的代碼,而是使用正確的時(shí)區(qū)名稱.示例:歐洲/巴黎"、美國(guó)/蒙特利爾"和亞洲/加爾各答".
Instead of 3-letter codes, use proper time zone names. Examples: "Europe/Paris", "America/Montreal", and "Asia/Kolkata".
似乎沒有時(shí)區(qū)名稱的官方標(biāo)準(zhǔn).這讓我感到驚訝;希望我錯(cuò)了,有人可以填寫.無論如何,常用??的列表來自 tz 數(shù)據(jù)庫(以前稱為 Olson 數(shù)據(jù)庫),如 在此維基百科頁面中列出.
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.
優(yōu)秀的日期時(shí)間庫,Joda-Time,有生成方法當(dāng)前已知時(shí)區(qū)名稱的列表.
The excellent date-time library, Joda-Time, has a method to generate a list of its currently known time zone names.
時(shí)區(qū)名稱會(huì)隨著時(shí)間的推移而變化,一些會(huì)被添加,它們的規(guī)則也會(huì)發(fā)生變化.這一切都是由政客和官僚決定的,所以改變是最后一刻的,并不總是明智的.所以你應(yīng)該注意保持你的日期時(shí)間庫是最新的,或者至少更新它包含的時(shí)區(qū)數(shù)據(jù)庫.
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.
時(shí)區(qū)不僅僅是與 UTC/GMT.時(shí)區(qū)還包含 夏令時(shí) (DST) 和其他異常情況的規(guī)則集.
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.
因此,您無法從偏移量推斷時(shí)區(qū).你可以猜測(cè),但你不能確定.
So you cannot infer a time zone from an offset. You can guess, but you cannot be sure.
例如取+01:00
的偏移量.那是歐洲/巴黎"還是非洲/拉各斯"?兩者都有 UTC 前一小時(shí)的偏移量.那么你使用哪個(gè)重要嗎?是的……法國(guó)遵守夏令時(shí),但尼日利亞沒有.分配錯(cuò)誤的時(shí)區(qū)意味著您的日期時(shí)間計(jì)算將是錯(cuò)誤的.
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.
另一個(gè)轉(zhuǎn)折……也許那個(gè) +01:00
是在夏季的倫敦錄制的.在夏季,倫敦遵守 DST 并將時(shí)鐘提前 1 小時(shí).雖然標(biāo)準(zhǔn)時(shí)間是 +00:00
(在 UTC/GMT 上),但 DST 將它們提前一小時(shí).
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.
又一個(gè)轉(zhuǎn)折點(diǎn)……即使你說隨便挑一個(gè)",是哪一個(gè)?對(duì)于標(biāo)準(zhǔn)時(shí)間的+00:00
,至少有2個(gè)三字母代碼(CET
和MET
)和37個(gè)命名時(shí)區(qū)跨越兩大洲.
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 在不同時(shí)區(qū)的不同日期開始和結(jié)束,共享相同的偏移量.此外,一些國(guó)家(時(shí)區(qū))足夠明智,不會(huì)被 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.
因此,關(guān)于您的問題不是一個(gè)簡(jiǎn)單的問題......但沒關(guān)系"是錯(cuò)誤的.這不是問題,這是不可能的.就像這個(gè)問題,給定一個(gè)生日,確定一個(gè)人".您可以確定一個(gè)人或時(shí)區(qū)不正確,但您無法確定哪個(gè)正確.
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.
如果了解時(shí)區(qū)(其位置和規(guī)則)對(duì)您很重要,則必須記錄時(shí)區(qū)信息以及日期時(shí)間.例如,這可能意味著您的數(shù)據(jù)庫中有一個(gè)額外的字段.
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.設(shè)計(jì)者已經(jīng)意識(shí)到時(shí)區(qū)作為日期時(shí)間值的一部分的重要性.因此,他們的設(shè)計(jì)包括:
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:
- 主要的日期時(shí)間類以Zoned"一詞開頭,以強(qiáng)調(diào)該類包含時(shí)區(qū)信息:ZonedDateTime
- 他們?cè)?ZonedDateTime 類上的
toString
實(shí)現(xiàn)擴(kuò)展了 ISO 8601 通過在括號(hào)中附加時(shí)區(qū)名稱來格式化.而不是:2014-02-14T20:51:55.427-08:00
它輸出2014-02-14T20:51:55.427-08:00[美國(guó)/洛杉磯]
- 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 outputs2014-02-14T20:51:55.427-08:00[America/Los_Angeles]
這篇關(guān)于基于Java中的UTC偏移獲取時(shí)區(qū)快捷方式的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!