-3

I want to parse the following time string to java.time:

OffsetDateTime.parse("00:00:00.000+02:00", DateTimeFormatter.ISO_OFFSET_TIME);

Result:

java.time.format.DateTimeParseException: Text '00:00:00.000+02:00' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {OffsetSeconds=7200},ISO resolved to 00:00 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
    at java.time.OffsetDateTime.parse(OffsetDateTime.java:402)

So how could I then parse this time, if not this way?

membersound
  • 81,582
  • 193
  • 585
  • 1,120

1 Answers1

4

An OffsetDateTime represents

an immutable representation of a date-time with an offset. This class stores all date and time fields, to a precision of nanoseconds, as well as the offset from UTC/Greenwich.

But you're trying to parse a date string without a date part

"00:00:00.000+02:00"

The parsing cannot extract a date from this and therefore fails.

You seem to want to use an OffsetTime

This class stores all time fields, to a precision of nanoseconds, as well as a zone offset.

You can then convert that to an OffsetDateTime, if you have a LocalDate, with OffsetTime#atDate.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724