Java Instant adjustInto() 方法

2025年1月7日 | 阅读 2 分钟

Java Instant 类的 adjustInto() 方法用于调整指定的 Temporal(接口)对象,使其具有此 Instant。

此方法返回一个与输入具有相同可观察类型的 temporal 对象,并将 instant 更改为与此相同。

语法

参数

temporal - 要调整的目标对象,非空

返回

调整后的对象,非空

调整相当于两次使用 Temporal.with(TemporalField, long),将 ChronoField.INSTANT_SECONDS 和 ChronoField.NANO_OF_SECOND 作为字段传递。

在大多数情况下,通过使用 Temporal.with(TemporalAdjuster) 来反转调用模式会更清晰。

异常

DateTimeException - 如果无法进行调整

ArithmeticException - 如果发生数值溢出

示例 1

输出

The output will be like this.
2014-12-03T10:15:30Z[Asia/New_Delhi]

示例 2

输出

2017-03-10T11:10:35.454+05:30[Asia/Calcutta]
2017-02-03T16:07:30+05:30[Asia/Calcutta]

示例 3

输出

2017-04-09T10:15:30Z
 OffsetDateTime > 2017-05-01T15:50:28.538-05:00 > 2017-04-09T05:15:30-05:00
  ZonedDateTime > 2017-05-01T15:50:28.540-05:00[Asia/New_Delhi] > 2017-04-09T05:15:30-05:00[Asia/New_Delhi]
        Instant > 2017-05-01T20:50:28.540Z > 2017-04-09T10:15:30Z