杰克逊使用+和t字符反序列化日期字符串

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

Jackson deserialize date string with + and t character

问题

我在反序列化 JSON 字符串日期时遇到问题:2020-09-30T07:00:00+02:00
我收到了错误消息:

com.fasterxml.jackson.databind.exc.InvalidFormatException: 无法从字符串 "2020-09-30T07:00:00+02:00" 反序列化为类型 `java.time.LocalDate`:无法反序列化 java.time.LocalDate:(java.time.format.DateTimeParseException) 无法解析文本 '2020-09-30T07:00:00+02:00',在索引 19 处找到未解析的文本

看起来好像是 + 符号是一个意外的字符。
POJO:

@JsonProperty("Date")
@JsonDeserialize(using = LocalDateDeserializer.class)
public LocalDate date;

有任何想法如何将这个字符串映射到 LocalDate 类型吗?

英文:

i have problem with deserialize Json string date: 2020-09-30T07:00:00+02:00
I am getting error:

com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String "2020-09-30T07:00:00+02:00": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '2020-09-30T07:00:00+02:00' could not be parsed, unparsed text found at index 19

Its look like + is and unexcepted character.
Pojo:

@JsonProperty("Date")
    @JsonDeserialize(using = LocalDateDeserializer.class)
    public LocalDate date;

Any ideas how i can map this string to localdate?

答案1

得分: 1

这应该可以运行

@JsonProperty("Date")
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssXXX")
public LocalDate date;

在这里查看日期格式:https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

此外,你应该使用另一种类型,如ZonedDateTime或java.util.Date,而不是LocalDate,以获得小时、分钟和秒。

英文:

This should work

@JsonProperty("Date")
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssXXX")
public LocalDate date;

Check DateFormat here: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Besides you should use another type like ZonedDateTime or java.util.Date instead of LocalDate to get hour, minute and second.

huangapple
  • 本文由 发表于 2020年9月30日 22:36:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64139888.html
匿名

发表评论

匿名网友

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

确定