Jackson @JsonFormat将日期缩短了一天

时间:2021-07-14 18:00:56

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".

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

The problem is that @JsonFormat set the date with one day less

问题是@JsonFormat将日期设置为少一天

This is my source code

这是我的源代码

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

Thanks

谢谢

2 个解决方案

#1


19  

Hey guys use this solution, it is more effective and modern than my solution.

嘿伙计们使用这个解决方案,它比我的解决方案更有效,更现代。

https://*.com/a/45456037/4886918

https://*.com/a/45456037/4886918

Thanks @Benjamin Lucidarme

谢谢@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

我将时区更改为“Brazil / East”或“America / Sao_Paulo”并立即开始工作

Thanks

谢谢

#2


5  

@William's answer works but you should add theses lines to your application.properties files instead:

@ William的答案有效,但您应该将这些行添加到application.properties文件中:

spring.jackson.time-zone=Brazil/East
spring.jackson.locale=pt-BR

In that way, you indicate the time-zone and locale only one time, and it applicate to all the Date of your application.

这样,您只需指定一次时区和区域设置,并且它适用于您的应用程序的所有日期。

#1


19  

Hey guys use this solution, it is more effective and modern than my solution.

嘿伙计们使用这个解决方案,它比我的解决方案更有效,更现代。

https://*.com/a/45456037/4886918

https://*.com/a/45456037/4886918

Thanks @Benjamin Lucidarme

谢谢@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

我将时区更改为“Brazil / East”或“America / Sao_Paulo”并立即开始工作

Thanks

谢谢

#2


5  

@William's answer works but you should add theses lines to your application.properties files instead:

@ William的答案有效,但您应该将这些行添加到application.properties文件中:

spring.jackson.time-zone=Brazil/East
spring.jackson.locale=pt-BR

In that way, you indicate the time-zone and locale only one time, and it applicate to all the Date of your application.

这样,您只需指定一次时区和区域设置,并且它适用于您的应用程序的所有日期。