为什么这个日期字符串无法通过验证?

huangapple go评论146阅读模式
英文:

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=&quot;UTC&quot;`,您应该使用 `ZonedDateTime`。请注意,在使用以下注释后,生成的日期时间将类似于 `2020-03-07T04:11:20.000 UTC`。

```java
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern = &quot;yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSS z&quot;, timezone=&quot;UTC&quot;)

您可以使用模式 yyyy-MM-dd&#39;T&#39;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 = &quot;yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSS z&quot;;
        String myText = &quot;2020-03-07T04:11:20.000 UTC&quot;;
        System.out.println(ZonedDateTime.parse(myText, DateTimeFormatter.ofPattern(myPattern)));
    }
}

输出:

2020-03-07T04:11:20Z[UTC]

如果您希望保持日期时间格式为 2020-03-07T04:11:20.000,则应该从注释中删除 timezone=&quot;UTC&quot;,并将获得的日期时间字符串解析为 LocalDateTime,而不是 ZonedDateTime。当然,此时模式应为 yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSS

英文:

First of all, you are using the wrong class, OffsetDateTime for your case. Since you have mentioned timezone=&quot;UTC&quot;, 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 = &quot;yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSS z&quot;, timezone=&quot;UTC&quot;)

which you can parse into ZonedDateTime using the pattern, yyyy-MM-dd&#39;T&#39;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 = &quot;yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSS z&quot;;
		String myText = &quot;2020-03-07T04:11:20.000 UTC&quot;;
		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=&quot;UTC&quot; 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&#39;T&#39;HH:mm:ss.SSS in that case.

huangapple
  • 本文由 发表于 2020年8月20日 20:40:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63505330.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定