2

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
Nuñito Calzada
  • 4,394
  • 47
  • 174
  • 301

2 Answers2

3

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);
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
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

developer_hatch
  • 15,898
  • 3
  • 42
  • 75