简体   繁体   English

@Temporal 在 Spring Boot 中格式化日期

[英]@Temporal to format date in Spring Boot

I have a date parameter in DB model and I want to retrive the date in this format "dd/MM/yyyy".我在 DB 模型中有一个日期参数,我想以这种格式“dd/MM/yyyy”检索日期。 I have annotation on getter like below:我在 getter 上有如下注释:

@CreationTimestamp
@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern = "dd/MM/yyyy")
public Date getCreated() {
    return created;
}

public void setCreated(Date created) {
    this.created = created;
}

However, I receive date in format yyyy-MM-dd.但是,我收到格式为 yyyy-MM-dd 的日期。 It seems that the @Temporal annotations formats to 'yyyy-MM-dd' but the DateTimeFormat has no effect.似乎@Temporal 注释格式为'yyyy-MM-dd',但DateTimeFormat 没有效果。

rest response:休息 回复:

{
   "id":"token1497340427913",
   "status":"finished",
   "startDate":null,
   "endDate":null,
   "created":"2017-06-13",
   "modified":"2017-06-13"
}

You might use @JsonFormat, look the chapter 3.2 http://www.baeldung.com/jackson-jsonformat您可能会使用@JsonFormat,请查看第 3.2 章http://www.baeldung.com/jackson-jsonformat

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ") 
private Date created;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM