我想改变一个即时的时间:使用java.time更换时间部分的时刻
Instant instant = Instant.parse("2016-03-23T17:14:00.092812Z");
LocalTime newTime = LocalTime.parse("12:34:45.567891");
instant.with(newTime);
我期望能获得与同日的瞬间,但随着新的时间,即2016-03-23 12:34:45.567891。
但它引发异常:
java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: NanoOfDay
at java.time.Instant.with(Instant.java:720)
at java.time.Instant.with(Instant.java:207)
at java.time.LocalTime.adjustInto(LocalTime.java:1333)
at java.time.Instant.with(Instant.java:656)
任何想法如何解决?
在即时设置时间没有多大意义。你需要一个时区来设置时间。将Instant转换为ZonedDateTime,选择时区。然后在ZonedDateTime上设置时间。然后将ZonedDateTime转换为即时。 –
@WilliMentzel我没有真正的代码中的字符串。我在这里解析它只是为了重现最小的代码。日期时间算术的字符串操作看起来很笨拙。 – kan