我正在寻求一些有关在 Spring 中格式化日期和时间戳的帮助。
我的属性注释如下:
@JsonFormat(pattern = "dd-MM-yy hh:mm")
@Column(name = "start_time")
private Date startTime;
然后传递到我的数据加载器中的实例
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yy hh:mm");
String startTimeSt = "05-11-2018 12:00";
Date startTime = null;
try {
startTime = dateFormat.parse(startTimeSt);
} catch (ParseException e) {
e.printStackTrace();
}
Booking booking1 = new Booking(customer1, gemma, startTime);
bookingRepository.save(booking1);
但它在我的 json 文件中返回,例如
"startTime": "2018-11-05T00:00:00.000+0000",
如何获得展示时间?
我还尝试将 spring.gson.date-format=dd-MM-yy hh:mm
传递到我的 application.properties 文件中。
谢谢
最佳答案
日期格式中的小时被指定为 HH(大写),而不是代码示例中的 hh - 请尝试在您的格式中使用 HH 并设置 @JsonFormat
注释的 shape 属性,如下所示:
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yy HH:mm")
请参阅以下有关 @JsonFormat
的教程:
关于java - Spring Json 日期和时间格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53169126/