预备知识
- 阳历(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包