Follow the documentation of DateTimeFormatter
:
DateTimeFormatter dtf
= DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss X uuuu", Locale.ROOT);
Instant asInstant = OffsetDateTime.parse(dateStr, dtf).toInstant();
With your example string the result is an Instant
of
2017-08-09T17:11:53Z
It 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 small x
or capital Z
instead of capital X
in the format pattern. If you want stricter validation, you may also check that the OffsetDateTime
’s .getOffset().equals(ZoneOffset.UTC)
yields true before you convert to Instant
.