【问题标题】:I want date to be displayed in Milliseconds(epoch) format but getting date with timestamp我希望日期以毫秒(纪元)格式显示,但获取带有时间戳的日期
【发布时间】:2019-05-07 19:42:06
【问题描述】:
@Data
public class SampleDate {
   private Date revisiondate;
}

@RequestMapping("/date")
public ResponseEntity<List<SampleDate>> getDateSample()
{
    List<SampleDate> listDate = new ArrayList<>();
    SampleDate sampDate = new SampleDate();
    sampDate.setRevisiondate(new Date());
    listDate.add(sampDate);
    return new ResponseEntity<>(listDate,HttpStatus.OK);
}

输出:[{"revisiondate":"2018-12-06T06:06:18.795+0000"}]

预期输出:[{"revisiondate":1544077577462}]

我使用的是 Springboot 2.0.3.RELEASE 版本。在 Springboot 1.3.2.RELEASE 版本中,我能够实现这一点。有没有人在升级 Springboot 时遇到过这种情况。

【问题讨论】:

  • 是什么让你想要那个输出?您已经拥有的那个更具可读性。输出采用国际标准 ISO 8601 格式,因此应该被广泛接受。
  • 实际上我们正在升级生产中的 SOA 服务(Springboot 1.3.2.RELEASE)。所以我们需要得到与生产代码完全相同的输出。

标签: java date spring-boot datetime epoch


【解决方案1】:

this guide来看,假设SpringBoot仍然使用Jackson,看起来你可以指定数据的形状应该是数字:

@JsonFormat(shape = JsonFormat.Shape.NUMBER)
private Date revisiondate;

(诚然,该指南将其描述为自 Unix 时代以来的 ,但它给出了一个毫秒的示例。)

【讨论】:

    【解决方案2】:

    您可以在日期字段顶部使用以下注释。

    @JsonFormat(shape = JsonFormat.Shape.NUMBER)
    private Date revisiondate;
    

    如建议here

    【讨论】:

      猜你喜欢
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2011-01-20
      • 1970-01-01
      • 2021-04-01
      • 2013-11-03
      相关资源
      最近更新 更多