Spring Json 日期和时间格式

Spring Json date and time formatting

我正在寻求有关格式化 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 注释的形状 属性 设置为如下:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yy HH:mm")

请参阅 @JsonFormat 上的以下教程:

https://www.baeldung.com/jackson-jsonformat