I have date in string "2015-05-08T02:56". As you can see it doesn't have seconds.
When I try to parse it with:
Instant instant = Instant.from(
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm")
.parse("2015-05-08T02:56"));
I get Exception
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
How to parse this date correctly and fill seconds with 0?
I forgot to specify timezone
Instant instant = Instant.from(
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm")
.withZone(ZoneId.of("UTC"))
.parse("2015-05-08T22:57"));
works fine.
UPDATE: Also I had another problem I used hh instead of HH.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With