问题
I have this piece of code:
public static final String DATE_PATTERN = "yyyy-MM-dd";
OffsetDateTime.parse(startTime, DateTimeFormatter.ofPattern(DateFormat.DATE_PATTERN)
But I have this error when parsing:
java.time.format.DateTimeParseException: Text '2019-07-10' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2019-07-10 of type java.time.format.Parsed
回答1:
ZonedDateTime
A date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris.
LocalDate
A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.
Since your string just represents simple date, so use LocalDate
LocalDate date = LocalDate.parse(startTime, DateTimeFormatter.ISO_DATE);
回答2:
The problem is that these parse methods need the offset string part (+/-hh:mm), if you want to use OffsetDateTime you need to add that part, here some examples:
OffsetDateTime date = OffsetDateTime.parse("2016-10-02T20:15:30+01:00",
DateTimeFormatter.ISO_DATE_TIME);
If you just want that format, "yyyy-mm-dd" you just need to go with traditional LocalDate.parse
来源:https://stackoverflow.com/questions/57208670/could-not-be-parsed-unable-to-obtain-offsetdatetime-from-temporalaccessor