有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java这个日期时间的JSON格式模式是什么?

这个Datetime字符串的JsonFormat模式是什么

"2021-02-16Z09:38:35"

它被定义为字符串(ISO日期时间格式)

我把它定义为-

@JsonProperty("lastDT")
@JsonFormat(pattern = "yyyy-MM-dd'Z'HH:mm:ss", shape = JsonFormat.Shape.STRING)
private ZonedDateTime lastDT;

我一直收到一个JSON解析错误

Failed to deserialize java.time.ZonedDateTime: (java.time.DateTimeException) Unable to obtain 
ZonedDateTime from TemporalAccessor: {},ISO resolved to 2021-02-16T09:38:35 of type java.time.format.Parsed

共 (1) 个答案

  1. # 1 楼答案

    ISO 8601格式,拙劣

    你问:

    What is the JsonFormat Pattern for this Datetime String?

    "2021-02-16Z09:38:35"

    似乎有人尝试了标准ISO 8601格式,但失败了

    在ISO8601中,字符串的日期部分和时间部分之间应该有一个T

    正确的格式应该是2021-02-16T09:38:35,而不是带有T而不是Z2021-02-16Z09:38:35

    我建议您教育数据源的发布者如何正确使用ISO 8601

    使用LocalDateTime,而不是ZonedDateTime

    类似2021-02-16T09:38:35的字符串表示日期和时间,但没有任何时区或UTC偏移的指示。因此,您应该将代码更改为使用LocalDateTime而不是ZonedDateTime

    String inputCorrected = "2021-02-16Z09:38:35".replace( "Z" , "T" ) ;
    LocalDateTime ldt = LocalDateTime.parse( inputCorrected ) ;
    

    祖鲁时间

    ISO 8601中使用Z表示与UTC的偏移量为0小时分秒。按照航空/军事传统,字母发音为“祖鲁”

    Instant instant = Instant.now() ;    // Represent a moment as seen in UTC, an offset of zero hours-minutes-seconds from UTC.
    String output = instant.toString() ;
    

    2021-02-16T09:38:35Z