概观
原文: https://docs.oracle.com/javase/tutorial/datetime/iso/overview.html
表示时间有两种基本方法。一种方式表示人类时间,称为人类时间,例如年,月,日,小时,分钟和秒。另一方面,机器时间沿着来自原点的时间线连续测量时间,称为时期,以纳秒分辨率。 Date-Time 包提供了丰富的类数组,用于表示日期和时间。 Date-Time API 中的某些类用于表示机器时间,而其他类更适合表示人工时间。
首先确定您需要的日期和时间的哪些方面,然后选择满足这些需求的类或类。选择基于时间的课程时,首先要确定是否需要表示人工时间或机器时间。然后,您可以确定需要表示的时间方面。你需要一个时区吗?日期和时间?仅限日期?如果你需要约会,你需要月,日,和年,还是一个子集?
Terminology: The classes in the Date-Time API that capture and work with date or time values, such as Instant
, LocalDateTime
, and ZonedDateTime
, are referred to as temporal-based classes (or types) throughout this tutorial. Supporting types, such as the TemporalAdjuster
interface or the DayOfWeek
enum, are not included in this definition.
例如,您可以使用LocalDate
对象来表示出生日期,因为大多数人在同一天观察他们的生日,无论他们是在他们的出生城市还是在全球的另一边。日期线。如果您正在跟踪占星时间,那么您可能希望使用LocalDateTime
对象来表示出生日期和时间,或使用ZonedDateTime
,其中还包括时区。如果您正在创建时间戳,那么您很可能想要使用Instant
,它允许您将时间线上的一个瞬时点与另一个瞬时点进行比较。
下表总结了存储日期和/或时间信息的java.time
包中基于时间的类,或者可用于测量时间量的类。列中的复选标记表示该类使用该特定类型的数据, toString 输出列显示使用toString
方法打印的实例。 讨论栏目链接到教程中的相关页面。
类或枚举 | 年 | 月 | 天 | 小时 | 分钟 | 秒* | 区域偏移 | 区域 ID | toString 输出 | 讨论的地方 |
---|---|---|---|---|---|---|---|---|---|---|
即时 |
| | | 2013-08-20T15:16:26.355Z
| 即时班 |
| LocalDate
|
|
|
| | | | | | 2013-08-20
| 日期类 |
| LocalDateTime
|
|
|
|
|
|
| | | 2013-08-20T08:16:26.937
| 日期和时间类 |
| ZonedDateTime
|
|
|
|
|
|
|
|
| 2013-08-21T00:16:26.941 + 09:00 [亚洲/东京]
| 时区和偏移类 |
| LocalTime
| | | |
|
|
| | | 08:16:26.943
| 日期和时间类 |
| 月日
| |
|
| | | | | | - 08-20
| 日期类 |
| 年
|
| | | | | | | | 2013
| 日期类 |
| YearMonth
|
|
| | | | | | | 2013-08
| 日期类 |
| 月
| |
| | | | | | | 8 月
| DayOfWeek 和月份枚举 |
| OffsetDateTime
|
|
|
|
|
|
|
| | 2013-08-20T08:16:26.954-07:00
| 时区和偏移类 |
| 偏移时间
| | | |
|
|
|
| | 08:16:26.957-07:00
| 时区和偏移类 |
| 持续时间
| | | | | ** |
| | | PT20H
(20 小时) | 期间和持续时间 |
| 期间
|
|
|
| | | | | | P10D
(10 天) | 期间和持续时间 |
| | 秒数被捕获到纳秒级精度。 |
| | 此类不存储此信息,但具有在这些单元中提供时间的方法。 |
| | 当Period
被添加到ZonedDateTime
时,观察到夏令时或其他本地时差。 |