別擔心,使用java.time
java.time
讓我們來看看使用java.time類加上6個小時的結果。
定義日期和時間部分。
LocalDate ld = LocalDate.of (2015, Month.OCTOBER, 24); // 24th Oct 2015 at 10:00am per the Question.
LocalTime lt = LocalTime.of (10, 0);
定義的時區,一個ZoneId
對象,Europe/London
。
ZoneId z = ZoneId.of ("Europe/London");
合併以創建ZonedDateTime
對象。
ZonedDateTime zdtStart = ZonedDateTime.of (ld, lt, z);
從ZonedDateTime
中提取Instant
。 Instant
類表示UTC中時間軸上的一個時刻,分辨率爲nanoseconds(小數點後最多九(9)位數字)。
Instant instantStart = zdtStart.toInstant ();
將我們的時間跨度6小時定義爲Duration
。 java.time類可以通過添加一個Duration
對象來執行日期 - 時間數學。
A Duration
未連接到時間線,實際上存儲了幾秒和幾納秒。所以在這個課堂上,「六小時」以及時鐘和DST等沒有智能。當我們要求六小時的Duration
時,該班級立即計算出(6小時* 60分鐘每小時* 60秒每分鐘)= 21,600秒。
Duration sixHours = Duration.ofHours (6); // 21,600 seconds = (6 hours * 60 minutes per hour * 60 seconds per minute).
循環十次。第一循環通過將Duration
添加到ZonedDateTime
,並將結果轉換爲Instant
。
// Increment the `ZonedDateTime`.
ZonedDateTime zdt = zdtStart;
for (int i = 1 ; i <= 10 ; i++) {
System.out.println (">zdt.toString() " + zdt + " | zdt.toInstant().toString(): " + zdt.toInstant () + "\n");
// Set up next loop.
zdt = zdt.plus (sixHours);
}
運行時。請注意倫敦時間的時間跳躍。這是Daylight Saving Time (DST)轉換,秋季時的「後退」時間,當英格蘭從標準時間從+01:00
的偏移量轉移到Zulu偏移量+00:00
,其中凌晨2點時鐘跳回到重複凌晨1點。所以,如果我們預計22:00加上6個小時導致4點,我們會看到3點。您可以在Instant
的價格中看到確實已經過去了六個小時。訣竅是,倫敦人在那個時候一小時左右就把鐘擺回來了。
查看history of DST cutovers的Europe/London
。
zdt.toString()2015-10-24T10:00 + 01:00 [Europe/London] | zdt.toInstant()。toString():2015-10-24T09:00:00Z
zdt.toString()2015-10-24T16:00 + 01:00 [Europe/London] | zdt.toInstant()。toString():2015-10-24T15:00:00Z
zdt.toString()2015-10-24T22:00 + 01:00 [Europe/London] | zdt.toInstant()。toString():2015-10-24T21:00:00Z
zdt.toString()2015-10-25T03:00Z [Europe/London] | zdt.toInstant()。toString():2015-10-25T03:00:00Z
zdt.toString()2015-10-25T09:00Z [Europe/London] | zdt.toInstant()。toString():2015-10-25T09:00:00Z
zdt.toString()2015-10-25T15:00Z [Europe/London] | zdt.toInstant()。toString():2015-10-25T15:00:00Z
zdt.toString()2015-10-25T21:00Z [Europe/London] | zdt.toInstant()。toString():2015-10-25T21:00:00Z
zdt.toString()2015-10-26T03:00Z [Europe/London] | zdt.toInstant()。toString():2015-10-26T03:00:00Z
zdt.toString()2015-10-26T09:00Z [Europe/London] | zdt.toInstant()。toString():2015-10-26T09:00:00Z
zdt.toString()2015-10-26T15:00Z [Europe/London] | 。zdt.toInstant()的toString():2015-10-26T15:00:00Z
對於我們交換的樂趣,增加了6個小時先後到Instant
和結果轉換爲倫敦時間。
// Increment the `Instant`.
Instant instant = instantStart;
for (int i = 1 ; i <= 10 ; i++) {
System.out.println (">instant.toString() " + instant + " | instant.atZone(z).toString(): " + instant.atZone (z) + "\n");
// Set up next loop.
instant = instant.plus (sixHours);
}
運行時,我們看到相同的值輸出。
instant.toString()2015-10-24T09:00:00Z | instant.atZone(z).toString():2015-10-24T10:00 + 01:00 [歐洲/倫敦]
instant.toString()2015-10-24T15:00:00Z | instant.atZone(z).toString():2015-10-24T16:00 + 01:00 [歐洲/倫敦]
instant.toString()2015-10-24T21:00:00Z | instant.atZone(z).toString():2015-10-24T22:00 + 01:00 [歐洲/倫敦]
instant.toString()2015-10-25T03:00:00Z | instant.atZone(z).toString():2015-10-25T03:00Z [歐洲/倫敦]
instant.toString()2015-10-25T09:00:00Z | instant.atZone(z).toString():2015-10-25T09:00Z [歐洲/倫敦]
instant.toString()2015-10-25T15:00:00Z | instant.atZone(z).toString():2015-10-25T15:00Z [歐洲/倫敦]
instant.toString()2015-10-25T21:00:00Z | instant.atZone(z).toString():2015-10-25T21:00Z [歐洲/倫敦]
instant.toString()2015-10-26T03:00:00Z | instant.atZone(z).toString():2015-10-26T03:00Z [歐洲/倫敦]
instant.toString()2015-10-26T09:00:00Z | instant.atZone(z).toString():2015-10-26T09:00Z [歐洲/倫敦]
instant.toString()2015-10-26T15:00:00Z | instant.atZone(z).toString():2015-10-26T15:00Z [歐洲/倫敦]
看到這個code run live at IdeOne.com。
關於java。時間
的java.time框架是建立在Java 8和更高版本。這些類取代了日期時間類legacy,如java.util.Date
,Calendar
,& SimpleDateFormat
。
Joda-Time項目現在位於maintenance mode,建議遷移到java.time類。請參閱Oracle Tutorial。並搜索堆棧溢出了很多例子和解釋。規格是JSR 310。
從何處獲取java.time類?
的ThreeTen-Extra項目與其他類擴展java.time。這個項目是未來可能增加java.time的一個試驗場。您可以在這裏找到一些有用的類,如Interval
,YearWeek
,YearQuarter
和more。
「容納」是什麼意思?如果您展示一個簡短但完整的程序來展示問題 - 包括實際產出和預期產出,這將非常有幫助。 –
在帖子中增加了更多信息。 – MisterIbbs
你還沒有添加一個簡短但完整的例子,這是我真的很想看到... –