@JsonFormat & @DateTimeFormat 前后台交互 时间戳转为年月日时间

@JsonFormat后台到前台的时间格式的转换
@DataFormAT前后到后台的时间格式的转换


1.@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
pattern:需要转换的时间日期的格式
timezone:是时间设置为东八区

2.@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
pattern:需要转换的时间日期的格式

3.例子
/*俩个注解可以同时使用*/
@ApiModelProperty("创建时间") @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date createdDate ;

举例:以下ClinicalTrialSettleParamVO 作为swagger入参,日期类型的列,可同时接收时间戳和时间类型的数据

若不加任何注解,默认接收时间戳,返回json也是long类型的时间戳

public class ClinicalTrialSettleParamVO {
    @Schema(description = "项目id", required = true, example = "LCSY2019091000000001")
    private String projectId;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss" ,timezone = "GMT+8")
    @Schema(description = "起始时间", required = true, example = "2019-11-01 00:00:00")
    private Date startTimestamp;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss" ,timezone = "GMT+8")
    @Schema(description = "结束时间", required = true, example = "2019-11-01 23:59:59")
    private Date endTimestamp;


    public ClinicalTrialSettleParamVO() {
    }

    public String getProjectId() {
        return projectId;
    }

    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    public Date getStartTimestamp() {
        return startTimestamp;
    }

    public void setStartTimestamp(Date startTimestamp) {
        this.startTimestamp = startTimestamp;
    }

    public Date getEndTimestamp() {
        return endTimestamp;
    }

    public void setEndTimestamp(Date endTimestamp) {
        this.endTimestamp = endTimestamp;
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章