問題描述
誰能說說三件事:
- 如何從 java.util.Date 實(shí)例中獲取 TimeZone?
- 如何知道夏令時(shí)是否適用?
我想我可以通過 timeZone.getDSTSavings 知道,但我面臨的問題是,即使我將系統(tǒng)的日期設(shè)為 2012 年 2 月 1 日,我仍然是將值設(shè)為正數(shù)(我猜是 3600000) - 如何將 EST 時(shí)間轉(zhuǎn)換為 EDT,反之亦然?
推薦答案
如何從 java.util.Date 實(shí)例中檢索 TimeZone?
How to retrieve TimeZone from java.util.Date instance?
沒有這樣的事情.Date
just 表示自 Unix 紀(jì)元(UTC 時(shí)間 1970 年 1 月 1 日午夜)以來的毫秒數(shù).它與特定的日歷系統(tǒng)或時(shí)區(qū)無關(guān).換句話說,如果我和一個(gè)朋友一起打電話(零延遲;)并且我點(diǎn)擊我的手指,我們都會(huì)同意該點(diǎn)擊的 Date
-即使我使用公歷而他使用儒略歷,即使我在倫敦而他在紐約.這是相同的即時(shí).
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.
如何知道夏令時(shí)是否適用?,我想我可以通過 timeZone.getDSTSavings 知道,但我面臨的問題是,即使我將系統(tǒng)的日期設(shè)為 2012 年 2 月 1 日,我仍然可以獲得價(jià)值為正(我猜是 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
內(nèi)您可以使用 TimeZone.getOffset(long)
查找與 UTC 的偏移量,或 TimeZone.inDaylightTime(Date)
只給你一個(gè)是/否的答案.
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 時(shí)間轉(zhuǎn)換為 EDT,反之亦然?
How to convert EST time to EDT or vice versa?
通常這是一個(gè)無效的問題 - 因?yàn)樵谌魏我粋€(gè)時(shí)間實(shí)例中, EST 或 EDT 適用.您通常會(huì)從一個(gè)時(shí)區(qū)轉(zhuǎn)換到另一個(gè)時(shí)區(qū),并且EDT"和EST"不是不同的時(shí)區(qū)——它們是相同時(shí)區(qū)內(nèi)的不同偏移量.您要求這樣做的事實(shí)表明您可能一開始就錯(cuò)誤地對(duì)數(shù)據(jù)進(jìn)行了建模(不幸的是,這對(duì)于日期/時(shí)間值很容易做到).請(qǐng)向我們提供更多信息,我們或許可以為您提供更多幫助.
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.
這篇關(guān)于在 Java 中將 EST 轉(zhuǎn)換為 EDT 或反之亦然的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!