562.md 6.1 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 概观

> 原文: [https://docs.oracle.com/javase/tutorial/datetime/iso/overview.html](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 输出 | 讨论的地方 |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| `即时` |  |  |  |  |  | 

W
wizardforcel 已提交
23
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
24 25 26 27

 |  |  | `2013-08-20T15:16:26.355Z` | [即时班](instant.html) |
| `LocalDate` | 

W
wizardforcel 已提交
28
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
29 30 31

 | 

W
wizardforcel 已提交
32
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
33 34 35

 | 

W
wizardforcel 已提交
36
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
37 38 39 40

 |  |  |  |  |  | `2013-08-20` | [日期类](date.html) |
| `LocalDateTime` | 

W
wizardforcel 已提交
41
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
42 43 44

 | 

W
wizardforcel 已提交
45
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
46 47 48

 | 

W
wizardforcel 已提交
49
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
50 51 52

 | 

W
wizardforcel 已提交
53
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
54 55 56

 | 

W
wizardforcel 已提交
57
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
58 59 60

 | 

W
wizardforcel 已提交
61
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
62 63 64 65

 |  |  | `2013-08-20T08:16:26.937` | [日期和时间类](datetime.html) |
| `ZonedDateTime` | 

W
wizardforcel 已提交
66
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
67 68 69

 | 

W
wizardforcel 已提交
70
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
71 72 73

 | 

W
wizardforcel 已提交
74
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
75 76 77

 | 

W
wizardforcel 已提交
78
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
79 80 81

 | 

W
wizardforcel 已提交
82
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
83 84 85

 | 

W
wizardforcel 已提交
86
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
87 88 89

 | 

W
wizardforcel 已提交
90
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
91 92 93

 | 

W
wizardforcel 已提交
94
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
95 96 97 98

 | `2013-08-21T00:16:26.941 + 09:00 [亚洲/东京]` | [时区和偏移类](timezones.html) |
| `LocalTime` |  |  |  | 

W
wizardforcel 已提交
99
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
100 101 102

 | 

W
wizardforcel 已提交
103
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
104 105 106

 | 

W
wizardforcel 已提交
107
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
108 109 110 111

 |  |  | `08:16:26.943` | [日期和时间类](datetime.html) |
| `月日` |  | 

W
wizardforcel 已提交
112
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
113 114 115

 | 

W
wizardforcel 已提交
116
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
117 118 119 120

 |  |  |  |  |  | `- 08-20` | [日期类](date.html) |
| `年` | 

W
wizardforcel 已提交
121
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
122 123 124 125

 |  |  |  |  |  |  |  | `2013` | [日期类](date.html) |
| `YearMonth` | 

W
wizardforcel 已提交
126
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
127 128 129

 | 

W
wizardforcel 已提交
130
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
131 132 133 134

 |  |  |  |  |  |  | `2013-08` | [日期类](date.html) |
| `月` |  | 

W
wizardforcel 已提交
135
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
136 137 138 139

 |  |  |  |  |  |  | `8 月` | [DayOfWeek 和月份枚举](enum.html) |
| `OffsetDateTime` | 

W
wizardforcel 已提交
140
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
141 142 143

 | 

W
wizardforcel 已提交
144
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
145 146 147

 | 

W
wizardforcel 已提交
148
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
149 150 151

 | 

W
wizardforcel 已提交
152
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
153 154 155

 | 

W
wizardforcel 已提交
156
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
157 158 159

 | 

W
wizardforcel 已提交
160
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
161 162 163

 | 

W
wizardforcel 已提交
164
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
165 166 167 168

 |  | `2013-08-20T08:16:26.954-07:00` | [时区和偏移类](timezones.html) |
| `偏移时间` |  |  |  | 

W
wizardforcel 已提交
169
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
170 171 172

 | 

W
wizardforcel 已提交
173
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
174 175 176

 | 

W
wizardforcel 已提交
177
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
178 179 180

 | 

W
wizardforcel 已提交
181
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
182 183 184 185

 |  | `08:16:26.957-07:00` | [时区和偏移类](timezones.html) |
| `持续时间` |  |  | ** | ** | ** | 

W
wizardforcel 已提交
186
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
187 188 189 190

 |  |  | `PT20H` (20 小时) | [期间和持续时间](period.html) |
| `期间` | 

W
wizardforcel 已提交
191
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
192 193 194

 | 

W
wizardforcel 已提交
195
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
196 197 198

 | 

W
wizardforcel 已提交
199
![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)
W
init  
wizardforcel 已提交
200 201 202 203 204 205

 |  |  |  | *** | *** | `P10D` (10 天) | [期间和持续时间](period.html) |

| * | 秒数被捕获到纳秒级精度。 |
| ** | 此类不存储此信息,但具有在这些单元中提供时间的方法。 |
| *** | 当 `Period` 被添加到 `ZonedDateTime` 时,观察到夏令时或其他本地时差。 |