SpringBoot JsonFormat DateTimeFormat 不生效

Restcontroller层输出的数据日期没有按照理想的格式显示,使用如下注解也未能达到要求 

    @DateTimeFormat(pattern = "yyyy-MM-dd" )
    @JsonFormat(pattern = "yyyy-MM-dd")

那么试试这个注解

    @JSONField(format = "yyyy-MM-dd")

当使用@RequestBody注解接收前端传来的数据时,@DateTimeFormat注解是不会生效的。因为@DateTimeFormat注解主要用于将前端传来的时间字符串转换为Date类,而@RequestBody注解接收到的是JSON格式的数据,其内部并没有使用到@DateTimeFormat注解进行转换。所以在这种情况下,你需要使用@JsonFormat注解来实现后端到前端时间格式的转换。@JsonFormat注解可以在实体类的字段上使用,用来指定该字段在返回给前端时的时间格式。同时,在前端展示后端数据时,如果希望格式化显示时间,也需要使用@JsonFormat注解来指定时间格式。所以,可以说@JsonFormat注解主要用于后端到前端的时间格式转换。但需要注意的是,如果直接使用System.out.println输出的数据仍然没有格式化,这是因为System.out.println输出的是对象的toString()方法返回的字符串,而不是JSON格式的数据。如果需要在控制台输出格式化的时间,可以使用SimpleDateFormat等工具类手动格式化时间。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [@JsonFormat与@DateTimeFormat](https://blog.csdn.net/xiaomisolo/article/details/104593972)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [@JsonFormat和@DateTimeFormat区别](https://blog.csdn.net/qq_45796667/article/details/125229282)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

as350144

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值