I am trying to parse the dates that come from the Twitter API into the new Instant
class from Java 8. FYI, this is the format Wed Aug 09 17:11:53 +0000 2017
.
I don’t want to deal with / set a time zone, as one is specified in the string already. I just want an Instant
instance.
2
Answers
Found the solution
Follow the documentation of
DateTimeFormatter
:With your example string the result is an
Instant
ofIt seems from the Twitter documentation that offset will always be
+0000
. Depending on how strongly you trust this to be the case, you may use smallx
or capitalZ
instead of capitalX
in the format pattern. If you want stricter validation, you may also check that theOffsetDateTime
’s.getOffset().equals(ZoneOffset.UTC)
yields true before you convert toInstant
.