【问题标题】:How do I format a Date as a Timestamp in Springboot 2?如何在 Spring Boot 2 中将日期格式化为时间戳?
【发布时间】:2020-02-28 17:53:42
【问题描述】:

在将对象序列化为 json 时,Spring boot 2 已将 UTC 格式作为日期的默认格式。这打破了我们依赖日期作为时间戳的几个旧集成。如何选择性地将此功能恢复为需要它的响应?

【问题讨论】:

  • 我可以得到负票的上下文吗?当我们将系统升级到 springboot 2 时,这导致了一个错误,因此这是一个严重的问题。

标签: java spring-boot spring-boot-2


【解决方案1】:

在您需要再次格式化为时间戳的任何日期,在构造函数或字段中使用 @JsonFormat(shape = JsonFormat.Shape.NUMBER) 对它们进行注释,如下所示:

@JsonFormat(shape = JsonFormat.Shape.Number)
private Date myDate;

MyClass(@JsonFormat(shape = JsonFormat.Shape.Number)
        Date myDate) {
...
}

【讨论】:

    猜你喜欢
    • 2019-08-10
    • 1970-01-01
    • 2022-11-26
    • 2020-06-22
    • 1970-01-01
    • 2018-06-15
    • 2023-03-05
    • 2018-03-27
    • 1970-01-01
    相关资源
    最近更新 更多