問題描述
誰能說說三件事:
- 如何從 java.util.Date 實例中獲取 TimeZone?
- 如何知道夏令時是否適用?
我想我可以通過 timeZone.getDSTSavings 知道,但我面臨的問題是,即使我將系統的日期設為 2012 年 2 月 1 日,我仍然是將值設為正數(我猜是 3600000) - 如何將 EST 時間轉換為 EDT,反之亦然?
推薦答案
如何從 java.util.Date 實例中檢索 TimeZone?
How to retrieve TimeZone from java.util.Date instance?
沒有這樣的事情.Date
just 表示自 Unix 紀元(UTC 時間 1970 年 1 月 1 日午夜)以來的毫秒數.它與特定的日歷系統或時區無關.換句話說,如果我和一個朋友一起打電話(零延遲;)并且我點擊我的手指,我們都會同意該點擊的 Date
-即使我使用公歷而他使用儒略歷,即使我在倫敦而他在紐約.這是相同的即時.
There's no such thing. A Date
just represents a number of milliseconds since the Unix epoch, which was midnight on January 1st 1970 UTC. It's not associated with a particular calendar system or time zone. To put it another way, if a friend and I are on the phone together (with a zero latency ;) and I click my fingers, we would both agree on the Date
at which that click too place - even if I'm using the Gregorian calendar and he's using the Julian calendar, and even if I'm in London and he's in New York. It's the same instant in time.
如何知道夏令時是否適用?,我想我可以通過 timeZone.getDSTSavings 知道,但我面臨的問題是,即使我將系統的日期設為 2012 年 2 月 1 日,我仍然可以獲得價值為正(我猜是 3600000)
How to know whether Daylight savings is applicable?, I suppose I can know it by doing timeZone.getDSTSavings, but problem I am facing is that even if I make my system's date as Feb 1 2012, still I am getting the value as positive (I guess 3600000)
理想情況下,使用 Joda Time 而不是 java.util.Date
/Calendar
/TimeZone
,但在 TimeZone
內您可以使用 TimeZone.getOffset(long)
查找與 UTC 的偏移量,或 TimeZone.inDaylightTime(Date)
只給你一個是/否的答案.
Ideally, use Joda Time instead of java.util.Date
/Calendar
/TimeZone
, but within TimeZone
you can use TimeZone.getOffset(long)
to find the offset from UTC, or TimeZone.inDaylightTime(Date)
to just give you a yes/no answer.
如何將 EST 時間轉換為 EDT,反之亦然?
How to convert EST time to EDT or vice versa?
通常這是一個無效的問題 - 因為在任何一個時間實例中, EST 或 EDT 適用.您通常會從一個時區轉換到另一個時區,并且EDT"和EST"不是不同的時區——它們是相同時區內的不同偏移量.您要求這樣做的事實表明您可能一開始就錯誤地對數據進行了建模(不幸的是,這對于日期/時間值很容易做到).請向我們提供更多信息,我們或許可以為您提供更多幫助.
Usually that's an invalid question - because at any one instance in time, either EST or EDT applies. You normally convert from one time zone to another, and "EDT" and "EST" aren't different time zones - they're different offsets within the same time zone. The fact that you're asking for this suggests that you may be modelling your data incorrectly to start with (which is unfortunately easy to do with date/time values). Please give us more information and we may be able to help you more.
這篇關于在 Java 中將 EST 轉換為 EDT 或反之亦然的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!