java.time包閱讀筆記
阿新 • • 發佈:2021-06-30
預備知識
- 陽曆(Solar Calendar), 根據地球圍繞太陽公轉軌道位置制定的歷法
- 陰曆(Lunar Calendar),按照月亮的月相週期制定的歷法
- 陰陽曆,朔望月指示月球的相位,年則與太陽有關
- 農曆(Chinese Calendar),是中國使用的陰陽曆,也稱黃曆,夏曆
- 格里曆(Gregorian Calendar), 陽曆的一種,也稱為公曆
- 公曆,中華人民共和國成立後,以格里曆為“公曆”,夏曆改稱“農曆”
- 格林尼治平均時間(英語:Greenwich Mean Time,GMT)是指位於英國倫敦郊區的皇家格林尼治天文臺當地的平太陽時。已經被原子鐘報時的協調世界時(UTC)所取代。
- ISO calendar system(ISO: 國際標準化組織)
- UTC
- 時區,東八區表示該地區時間比UTC時間快8小時
java.time包概述
- 所有的class都是immutable和thread-safe
各個包之間的關係
- java.time.temporal提供對時間的低層級訪問
- java.time.format提供格式
- java.time.chrono包含日曆無關(calendar neutral)的API
Dates and Times
Instant
- a numeric timestamp
LocalDate
LocalTime
LocalDateTime
ZonedDateTime
-
一般不需要使用
-
"Where possible, it is recommended to use a simpler class without a time-zone. The widespread use of time-zones tends to add considerable complexity to an application."
Duration and Period
Duration
- 以納秒錶示的時間差
Period
- 以對人類有意義的時間單位(年月日)表示的時間差
Additional value types
Month
- 例如,’DECEMBER‘
DayOfWeek
- 例如,’THUESDAY‘
Year
- 例如,’2021‘
YearMonth
- 例如,’2021-07‘
MonthYear
- 例如,‘--07-01’
OffsetTime
- 例如,‘15:07:46.964842600+08:00’
- 表示15:07分,目前處於東八區
Design Note
-
Instant是Java.util.Date的最接近的等價類
-
ZonedDateTime是java.util.GregorianCalendar的最接近的等價類
-
OffsetTime和OffsetDateTime主要用於網路協議和資料庫訪問,因為大多數資料庫無法將諸如‘Europe/Paris’這樣的時區資訊進行自動轉換並存儲,但是對於偏移卻可以
-
統一的方法介面
-
of - static factory method parse - static factory method focussed on parsing get - gets the value of something is - checks if something is true with - the immutable equivalent of a setter plus - adds an amount to an object minus - subtracts an amount from an object to - converts this object to another type at - combines this object with another, such as date.atTime(time)
-
-
資料以ISO Calendar標準儲存
-
不同日期之間的轉換需要使用到java.time.chrono包