Jackson @JsonFormat 设置日期少一天 [英] Jackson @JsonFormat set date with one day less

查看:34
本文介绍了Jackson @JsonFormat 设置日期少一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用了 Spring Date Rest 和 Spring Boot.这个项目有一个对象,我使用 @JsonFormat 注释来格式化将从我的 Json 接收的日期字段.字段日期的格式为dd/MM/yyyy".当我发送我的 json 值08/07/1980"时,Jackson 将转换为值07/07/1980".

I have been used Spring Date Rest with Spring Boot in my project. This project has a object and I have used the annotation @JsonFormat to format the date field that will be received from my Json. The format of field Date is "dd/MM/yyyy". When I send in my json the value "08/07/1980" the Jackson convert to the value "07/07/1980".

问题是@JsonFormat 设置的日期少了一天

这是我的源代码

@Temporal(TemporalType.DATE)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy", locale = "pt-BR", timezone = "UTC")
private Date birthDate;

谢谢

推荐答案

使用这个解决方案,它比我的解决方案更有效和现代:https://stackoverflow.com/a/45456037/4886918

Use this solution, it is more effective and modern than my solution: https://stackoverflow.com/a/45456037/4886918

感谢@Benjamin Lucidarme.

Thanks @Benjamin Lucidarme.

我使用以下方法解决了我的问题:

I resolved my problem using:

@Temporal(TemporalType.DATE)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy", locale = "pt-BR", timezone = "Brazil/East")
private Date birthDate;

我将时区更改为巴西/东部"或美国/圣保罗",现在正在工作

I changed timezone to "Brazil/East" or "America/Sao_Paulo" and working now

谢谢

这篇关于Jackson @JsonFormat 设置日期少一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆