If the offset Z
is optional, you can use java.time.format.DateTimeFormatterBuilder
with the optional section:
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart().appendOffsetId()
.toFormatter();
parseBest
TemporalQuery
, . :
Instant instant = null;
TemporalAccessor parsed = fmt.parseBest("2017-09-11T12:44:07.793Z", Instant::from, LocalDateTime::from);
if (parsed instanceof Instant) {
instant = (Instant) parsed;
} else if (parsed instanceof LocalDateTime) {
instant = ((LocalDateTime) parsed).atOffset(ZoneOffset.UTC).toInstant();
}
System.out.println(instant);
(0001-01-01T00:00:00
) Instant
0001-01-01T00:00:00Z
.
Instant::from
LocalDateTime::from
, Instant
. , LocalDateTime
. , (, ZonedDateTime::from
, ZonedDateTime
, Instant
toInstant()
).
, UTC, :
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart().appendOffsetId()
.toFormatter().withZone(ZoneOffset.UTC);
, Instant
:
System.out.println(Instant.from(fmt.parse("2017-09-11T12:44:07.793Z")));
System.out.println(Instant.from(fmt.parse("0001-01-01T00:00:00")));
user7605325