我试图将分区日期时间格式"2020-02-26T11:02:41 +0000“作为请求参数传递给我的spring引导控制器。
public ResponseEntity<?> getStatistics(
@ApiParam(value = "startDate", example = "2020-02-26T11:02:41 +0000")
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss 'Z'") @RequestParam(value = "startDate",
required = false) ZonedDateTime startDate)我在访问api时得到了这个异常。
Caused by: java.lang.IllegalArgumentException: Parse attempt failed for value [2020-02-26T11:02:41 0000]
at org.springframework.format.support.FormattingConversionService$ParserConverter.convert(FormattingConversionService.java:206)
at org.springframework.format.support.FormattingConversionService$AnnotationParserConverter.convert(FormattingConversionService.java:321)
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:40)
Caused by: java.time.format.DateTimeParseException: Text '2020-02-26T11:02:41 0000' could not be parsed at index 20
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)我到底错过了什么?spring似乎无法将字符串解析为datetime。
发布于 2020-03-29 07:41:10
四项建议:
ZonedDateTime,而且OffsetDateTime更正确,我建议您使用。Z,因为您已经将它用单引号括起来了。相反,我建议xx格式模式为四位偏移(如果您没有冒号之间的时间和分钟偏移)。URL编码(也称为百分比编码)用于对URL参数的字符串进行编码,以适应URL或URI语法,并确保即使在有限的字符集中也正确传输它们。除其他外,由于URL不允许包含空格,因此URL编码将空间更改为+ (加号)。因此,URL解码将所有加号转换回空格。你的绳子就是这样的。只有你的加号才意味着作为你的偏移量的一部分,+0000。所以不能再被解析了。您的异常消息引用了index 20,这正是加号应该在的位置。
链接
发布于 2020-03-27 13:45:53
您正在将文字'Z'作为区域偏移量传递.更改为yyyy-MM-dd'T'HH:mm:ssZ,然后传入2020-02-26T11:02:41+0000应进行修复。
https://stackoverflow.com/questions/60884883
复制相似问题