バーデン:
私が行った場合
import java.time.Instant;
...
Instant instant = Instant.parse("2018-01-02T18:14:59.000+01:00")
私はこの例外を取得します:
Caused by: java.time.format.DateTimeParseException: Text '2018-06-19T23:00:00.000+01:00' could not be parsed at index 23
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1777)
しかし、私は行った場合
Instant instant = Instant.parse("2018-06-19T23:00:00.000Z");
すべてはうまく動作します。
私は何を欠場か?初めての文字列でいただきましたが間違っ?
azro:
その理由は、あなたが最初ということであるString
ため許容できるフォーマットと一致しないparse
方法を
doc状態としてInstant#parse(CharSequence text)
:
文字列は、UTCでの有効な瞬間を表現しなければなりませんし、使用して解析されDateTimeFormatter.ISO_INSTANTを
そしてのためのドキュメントDateTimeFormatter#ISO_INSTANT
の状態:
「:15:30Z 2011-12-03T10」など、その形式やUTCインスタントを解析し、フォーマッタISOの瞬間。
取得するにはInstant
、あなたから文字列を次のものが必要です。Workable Demo
String str = "2018-01-02T18:14:59.000+01:00";
Instant instant = DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(str, Instant::from);