問題描述
我想根據 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個三字母代碼(CET
和MET
)和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 outputs2014-02-14T20:51:55.427-08:00[America/Los_Angeles]
這篇關于基于Java中的UTC偏移獲取時區快捷方式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!