如何解析具有毫秒和偏移量的时间?

How to parse time having milliseconds and offset?

我想将以下时间字符串解析为java.time:

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

结果:

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)

如果不是这样,那么这次我该如何解析呢?

一个OffsetDateTime代表

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.

但是您正在尝试解析没有日期部分的日期字符串

"00:00:00.000+02:00"

解析无法从中提取日期,因此失败。

您似乎想使用 OffsetTime

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

然后您可以将其转换为 OffsetDateTime,如果您有 LocalDateOffsetTime#atDate