ZonedDateTime
類表示帶有時(shí)區(qū)規(guī)則的日期時(shí)間。
ZonedDateTime
結(jié)合了 LocalDateTime
和 ZoneId
。
下面說(shuō)明如何從 LocalDateTime
創(chuàng)建 ZonedDateTime
。
import java.time.LocalDateTime; import java.time.Month; import java.time.ZoneId; import java.time.ZonedDateTime; public class Main { public static void main(String[] args) { ZoneId usCentral = ZoneId.of("America/Chicago"); LocalDateTime localDateTime = LocalDateTime.of(2014, Month.MAY, 21, 9, 30); System.out.println(localDateTime); ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, usCentral); System.out.println(zonedDateTime); } }
上面的代碼生成以下結(jié)果。
由于夏令時(shí)更改,可能會(huì)發(fā)生時(shí)區(qū)中的本地時(shí)間軸上的間隙或重疊。
當(dāng)時(shí)鐘向前或向后移動(dòng)一個(gè)小時(shí)時(shí),會(huì)有時(shí)間上的間隙或重疊
如果時(shí)間落在間隙的中間,則時(shí)間向前移動(dòng)與間隙相同的量。
如果時(shí)間落在重疊的中間,則時(shí)間有效。
默認(rèn)情況下,使用較早版本。 withEarlierOffsetAtOverlap()和fromLaterOffsetAtOverlap()從ZonedDateTime讓您選擇所需的區(qū)域偏移量,如果時(shí)間落在重疊。以下代碼顯示ZonedDateTime,其中時(shí)間落在間隙和重疊中。
import java.time.LocalDateTime; import java.time.Month; import java.time.ZoneId; import java.time.ZonedDateTime; public class Main { public static void main(String[] args) { ZoneId usChicago = ZoneId.of("America/Chicago"); // 2014-03-09T02:30 did not exist in America/Chicago time zone LocalDateTime ldt = LocalDateTime.of(2014, Month.MARCH, 9, 2, 30); ZonedDateTime zdt = ZonedDateTime.of(ldt, usChicago); System.out.println(zdt); // 2013-10-03T01:30 existed twice in America/Chicago time zone LocalDateTime ldt2 = LocalDateTime.of(2013, Month.NOVEMBER, 3, 1, 30); ZonedDateTime zdt2 = ZonedDateTime.of(ldt2, usChicago); System.out.println(zdt2.withEarlierOffsetAtOverlap()); System.out.println(zdt2.withLaterOffsetAtOverlap()); } }
上面的代碼生成以下結(jié)果。
ZonedDateTime.of Local(LocalDateTime localDateTime,ZoneId zone,Zone Offset preferred Offset)從區(qū)域偏移創(chuàng)建ZonedDateTime。
如果指定的引用區(qū)域偏移無(wú)效,則使用重疊的較早區(qū)域偏移。
-07:00無(wú)效,則使用較早的偏移量-05:00。
import java.time.LocalDateTime; import java.time.Month; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; public class Main { public static void main(String[] args) { ZoneId usChicago = ZoneId.of("America/Chicago"); ZoneOffset offset5 = ZoneOffset.of("-05:00"); ZoneOffset offset6 = ZoneOffset.of("-06:00"); ZoneOffset offset7 = ZoneOffset.of("-07:00"); LocalDateTime ldt = LocalDateTime.of(2012, Month.NOVEMBER, 4, 1, 30); ZonedDateTime zdt5 = ZonedDateTime.ofLocal(ldt, usChicago, offset5); ZonedDateTime zdt6 = ZonedDateTime.ofLocal(ldt, usChicago, offset6); ZonedDateTime zdt7 = ZonedDateTime.ofLocal(ldt, usChicago, offset7); System.out.println("With offset " + offset5 + ": " + zdt5); System.out.println("With offset " + offset6 + ": " + zdt6); System.out.println("With offset " + offset7 + ": " + zdt7); } }
上面的代碼生成以下結(jié)果。
以下代碼顯示了如何將ZonedDateTime內(nèi)容到本地和偏移量日期,時(shí)間和日期時(shí)間。
import java.time.LocalDateTime; import java.time.Month; import java.time.ZoneId; import java.time.ZonedDateTime; public class Main { public static void main(String[] args) { ZonedDateTime zdt1 = ZonedDateTime.now(); System.out.println("Current zoned datetime:" + zdt1); LocalDateTime ldt = LocalDateTime.of(2012, Month.MARCH, 11, 7, 30); ZoneId usCentralZone = ZoneId.of("America/Chicago"); ZonedDateTime zdt2 = ZonedDateTime.of(ldt, usCentralZone); System.out.println(zdt2); } }
上面的代碼生成以下結(jié)果。
當(dāng)您添加一天的持續(xù)時(shí)間時(shí),它將始終添加24小時(shí),無(wú)論該天有多少小時(shí)(23,24或25小時(shí))。
當(dāng)從正常時(shí)間改變?yōu)槿展夤?jié)約時(shí)間或返回時(shí),發(fā)生23和25小時(shí)。 當(dāng)進(jìn)入日光節(jié)約時(shí)間時(shí),我們失去一個(gè)小時(shí)。 當(dāng)離開日光節(jié)約時(shí)間,我們得到一個(gè)小時(shí)額外。
在2012-03-11T02:00,美國(guó)中部時(shí)區(qū)通過(guò)將時(shí)鐘向前移動(dòng)一小時(shí)進(jìn)入日光節(jié)約,使2012-03-11的時(shí)間為23小時(shí)。
import java.time.Duration; import java.time.LocalDateTime; import java.time.Month; import java.time.ZoneId; import java.time.ZonedDateTime; public class Main { public static void main(String[] args) { ZoneId usCentral = ZoneId.of("America/Chicago"); LocalDateTime ldt = LocalDateTime.of(2012, Month.MARCH, 10, 7, 30); ZonedDateTime zdt1 = ZonedDateTime.of(ldt, usCentral); Duration d1 = Duration.ofHours(24); ZonedDateTime zdt2 = zdt1.plus(d1); System.out.println(zdt2); } }
上面的代碼生成以下結(jié)果。
當(dāng)向ZonedDateTime添加一天的時(shí)間段時(shí),日期組件將更改為第二天,而不會(huì)影響時(shí)間,無(wú)論該天有多少小時(shí)(23小時(shí),24小時(shí)或25小時(shí))。
當(dāng)從正常時(shí)間改變?yōu)槿展夤?jié)約時(shí)間或返回時(shí),發(fā)生23和25小時(shí)。 當(dāng)進(jìn)入日光節(jié)約時(shí)間時(shí),我們失去一個(gè)小時(shí)。 當(dāng)離開日光節(jié)約時(shí)間,我們得到一個(gè)小時(shí)額外。
在2012-03-11T02:00,美國(guó)中部時(shí)區(qū)通過(guò)將時(shí)鐘向前移動(dòng)一小時(shí)進(jìn)入日光節(jié)約,使2012-03-11的時(shí)間為23小時(shí)。
import java.time.LocalDateTime; import java.time.Month; import java.time.Period; import java.time.ZoneId; import java.time.ZonedDateTime; public class Main { public static void main(String[] args) { ZoneId usCentral = ZoneId.of("America/Chicago"); LocalDateTime ldt = LocalDateTime.of(2012, Month.MARCH, 10, 7, 30); ZonedDateTime zdt1 = ZonedDateTime.of(ldt, usCentral); Period p1 = Period.ofDays(1); ZonedDateTime zdt2 = zdt1.plus(p1); System.out.println(zdt2); } }
上面的代碼生成以下結(jié)果。
更多建議: