无法从临时助理获取区域偏移量:{},ISO,欧洲/柏林已解析为2019-12-26,类型为java.time.format.Parsed
我试图将字符串解析为无法从临时助理获取区域偏移量:{},ISO,欧洲/柏林已解析为2019-12-26,类型为java.time.format.Parsed,java,spring-boot,datetime,jpa,Java,Spring Boot,Datetime,Jpa,我试图将字符串解析为OffsetDateTime,但出现以下错误: Unhandled exception. java.time.format.DateTimeParseException: Text '26122019' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO,Europe/Berlin resolved to 2019-12-26 of type java.time.
OffsetDateTime
,但出现以下错误:
Unhandled exception.
java.time.format.DateTimeParseException: Text '26122019' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO,Europe/Berlin resolved to 2019-12-26 of type java.time.format.Parsed
Caused by: java.time.DateTimeException: Unable to obtain OffsetDateTime from TemporalAccessor: {InstantSeconds=1577318400},ISO,Z resolved to 2019-12-26T00:00 of type java.time.format.Parsed
我试图解析的字符串示例类似于26122019
,数据库中的值类似于2018-08-31
我之前在为这些值编写JPA查询时遇到另一个错误,该错误将我发送到此路径@Param(“filterEndDate”)OffsetDateTime filterEndDate,
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMyyyy").withZone(ZoneId.of("Europe/Berlin"));
OffsetDateTime fromDate = OffsetDateTime.parse(filterFromDate,formatter);
OffsetDateTime toDate = OffsetDateTime.parse(filterEndDate,formatter);
然后我调整了代码
DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("ddMMyyyy")
.parseDefaulting(ChronoField.NANO_OF_DAY, 0)
.toFormatter()
.withZone(ZoneOffset.UTC);
并得到以下错误:
Unhandled exception.
java.time.format.DateTimeParseException: Text '26122019' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO,Europe/Berlin resolved to 2019-12-26 of type java.time.format.Parsed
Caused by: java.time.DateTimeException: Unable to obtain OffsetDateTime from TemporalAccessor: {InstantSeconds=1577318400},ISO,Z resolved to 2019-12-26T00:00 of type java.time.format.Parsed
-----更新1----
代码
错误
我展示了两种方式
解析为LocalDate并转换为
对我来说,简单的方法如下:
String filterFromDate = "26122019";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMyyyy");
OffsetDateTime fromDate = LocalDate.parse(filterFromDate, formatter)
.atStartOfDay(ZoneOffset.UTC)
.toOffsetDateTime();
System.out.println(fromDate);
此代码段的输出为:
2019-12-26T00:00Z
由于您的字符串包含日期、时间和偏移量,因此我将解析为LocalDate
。然后我执行到OffsetDateTime
的转换
调整高级格式化程序以执行此任务
您尝试的方法只需简单调整即可实现:
DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("ddMMyyyy")
.parseDefaulting(ChronoField.NANO_OF_DAY, 0)
.parseDefaulting(ChronoField.OFFSET_SECONDS, 0)
.toFormatter();
OffsetDateTime fromDate = OffsetDateTime.parse(filterFromDate, formatter);
结果和以前一样。time区分偏移量和时区。在许多需要时区的地方,可以使用偏移量,但在这里不行。对
withZone()
的调用提供了默认时区,但没有默认偏移量。相反,我使用.parseDefaulting(ChronoField.OFFSET\u SECONDS,0)
来建立默认偏移量。您想做什么?那么您的预期输出是什么?@Deadpool使用JPA查询一个格式为2018-08-31
的表的数据库。然后只需使用LocalDate
就可以了,您不需要OffsetDateTime
,像这样的`LocalDate.parse(inputString,DateTimeFormatter.ofPattern(“ddMMyyyy”))当我尝试这样做时,我得到一个错误:“参数值[2019-12-20]与预期类型[java.time.OffsetDateTime(n/a)]不匹配;嵌套异常为java.lang.IllegalArgumentException:参数值[2019-12-20]与预期类型[java.time.OffsetDateTime(n/a)]不匹配```