英文:
Why is this Date string failing the validation?
问题
我有一个JSON文件,其中一个字段的值为2020-03-07T04:11:20.000,其模式为yyyy-MM-dd'T'HH:mm:ss.SSS。我尝试在末尾添加一个Z,但始终无法通过验证。有任何想法为什么会这样?
我尝试运行以下代码:
OffsetDateTime.parse(mytext, DateTimeFormatter.ofPattern(mypattern))
但是它抛出了一个DateTimeParseException,错误消息为“无法从TemporalAccessor获取OffsetDateTime”。
英文:
I have a JSON file with a value of 2020-03-07T04:11:20.000 for a field with given pattern
yyyy-MM-dd'T'HH:mm:ss.SSS. I tried to add a Z at the end of it, but it keeps failing the validation.
Any idea why?
I tried to do
OffsetDateTime.parse(mytext, DateTimeFormatter.ofPattern(mypattern))
and it threw DateTimeParseException with Unable to obtain OffsetDateTime from TemporalAccessor
答案1
得分: 4
由于模式和输入均没有时区偏移量,您无法直接解析为 OffsetDateTime
。您可以将日期解析为 LocalDateTime
,然后将偏移量添加到结果中。
例如,使用 ZoneOffset.UTC
:
LocalDateTime ldt = LocalDateTime.parse(mytext, DateTimeFormatter.ofPattern(mypattern));
OffsetDateTime odt = ldt.atOffset(ZoneOffset.UTC);
英文:
Since neither the pattern nor the input has a timezone offset, you can't parse it directly to OffsetDateTime
. What you can do is parse the date as LocalDateTime
and add the offset to the result.
For example, using ZoneOffset.UTC:
LocalDateTime ldt = LocalDateTime.parse(mytext, DateTimeFormatter.ofPattern(mypattern));
OffsetDateTime odt = ldt.atOffset(ZoneOffset.UTC)
</details>
# 答案2
**得分**: 2
首先,您在这种情况下使用了错误的类,应该使用 `ZonedDateTime` 而不是 `OffsetDateTime`。由于您提到了 `timezone="UTC"`,您应该使用 `ZonedDateTime`。请注意,在使用以下注释后,生成的日期时间将类似于 `2020-03-07T04:11:20.000 UTC`。
```java
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS z", timezone="UTC")
您可以使用模式 yyyy-MM-dd'T'HH:mm:ss.SSS z
将其解析为 ZonedDateTime
。
示例:
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String myPattern = "yyyy-MM-dd'T'HH:mm:ss.SSS z";
String myText = "2020-03-07T04:11:20.000 UTC";
System.out.println(ZonedDateTime.parse(myText, DateTimeFormatter.ofPattern(myPattern)));
}
}
输出:
2020-03-07T04:11:20Z[UTC]
如果您希望保持日期时间格式为 2020-03-07T04:11:20.000
,则应该从注释中删除 timezone="UTC"
,并将获得的日期时间字符串解析为 LocalDateTime
,而不是 ZonedDateTime
。当然,此时模式应为 yyyy-MM-dd'T'HH:mm:ss.SSS
。
英文:
First of all, you are using the wrong class, OffsetDateTime
for your case. Since you have mentioned timezone="UTC"
, you should use ZonedDateTime
. Note that after using the following annotation, the date-time produced will be like 2020-03-07T04:11:20.000 UTC
.
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS z", timezone="UTC")
which you can parse into ZonedDateTime
using the pattern, yyyy-MM-dd'T'HH:mm:ss.SSS z
.
Demo:
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String myPattern = "yyyy-MM-dd'T'HH:mm:ss.SSS z";
String myText = "2020-03-07T04:11:20.000 UTC";
System.out.println(ZonedDateTime.parse(myText, DateTimeFormatter.ofPattern(myPattern)));
}
}
Output:
2020-03-07T04:11:20Z[UTC]
If you want to keep the date-time format as 2020-03-07T04:11:20.000
then you should remove timezone="UTC"
from the annotation and parse the obtained date-time string into a LocalDateTime
instead of ZonedDateTime
. Needless to say that the pattern should be yyyy-MM-dd'T'HH:mm:ss.SSS
in that case.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论