我有日期字符串"2015-05-08T02:56"
。正如你所看到的,它沒有秒。解析即時無秒
當我嘗試分析它:
Instant instant = Instant.from(
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm")
.parse("2015-05-08T02:56"));
我得到異常
Exception in thread "main" java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {MinuteOfHour=56, HourOfAmPm=2},ISO resolved to 2015-05-08 of type java.time.format.Parsed
at java.time.Instant.from(Instant.java:378)
at sample.Main.main(Main.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: InstantSeconds
at java.time.format.Parsed.getLong(Parsed.java:203)
at java.time.Instant.from(Instant.java:373)
... 6 more
如何正確地分析這個日期,並填寫0
秒?
將模式更改爲'「yyyy-MM-dd'T'HH:mm」',其中'HH'爲大寫。 – Andreas
@Andreas,爲什麼它在'Instant.from(TemporalAccessor)'中拒絕AM/PM格式' – AxelH
@AxelH因爲'hh'(01-12)沒有'a'(AM/PM)是一個不完整的小時值。輸入'02:00'可以是上午2點或下午2點。要正確解析每天的小時,您需要'HH'或'hh' +'a'。 – Andreas