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

本文详细介绍如何使用@JsonFormat和@DateTimeFormat注解在Java中实现前后端时间格式的转换,确保时间数据在不同系统间准确传递。通过具体示例,展示了如何在类属性上应用这些注解,以支持时间戳和日期类型数据的接收。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@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;
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值