could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: [duplicate]

拥有回忆 提交于 2019-12-11 06:47:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!