Jackson @JsonFormat 设置日期少一天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31822877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hymanson @JsonFormat set date with one day less
提问by William Miranda
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 Hymanson convert to the value "07/07/1980".
我在我的项目中使用了 Spring Date Rest 和 Spring Boot。这个项目有一个对象,我使用了 @JsonFormat 注释来格式化将从我的 Json 接收到的日期字段。字段日期的格式为“dd/MM/yyyy”。当我发送我的 json 值“08/07/1980”时,Hymanson 转换为值“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
谢谢
回答by William Miranda de Jesus
Use this solution, it is more effective and modern than my solution: https://stackoverflow.com/a/45456037/4886918
使用此解决方案,它比我的解决方案更有效、更现代:https: //stackoverflow.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
我将时区更改为“巴西/东部”或“美国/圣保罗”,现在正在工作
Thanks
谢谢
回答by Benjamin Lucidarme
@William's answer works but you should add theses lines to your application.properties files instead:
@William 的回答有效,但您应该将这些行添加到您的 application.properties 文件中:
spring.Hymanson.time-zone=Brazil/East
spring.Hymanson.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.
这样,您只指示一次时区和区域设置,它适用于您的应用程序的所有日期。
回答by Mehdi
On both side (Client - Server) annotate your date filed like this:
在两侧(客户端 - 服务器)注释您提交的日期,如下所示:
@JsonDeserialize(using = JsonDateDeserializer.class)
@JsonSerialize(using = JsonDateSerializer.class)
private Date birthDate;
and on both side again put this implementations for serializing and deserializing:
并在双方再次将这个实现用于序列化和反序列化:
public class JsonDateSerializer extends JsonSerializer<Date> {
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
@Override
public void serialize(final Date date, final JsonGenerator gen, final SerializerProvider provider) throws IOException, JsonProcessingException {
String dateString = format.format(date);
gen.writeString(dateString);
}
}
public class JsonDateDeserializer extends JsonDeserializer<Date> {
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
@Override
public Date deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
if (jp.getCurrentToken().equals(JsonToken.VALUE_STRING)) {
try {
Date date = format.parse(jp.getText().toString());
return date;
} catch (ParseException e) {
//e.printStackTrace();
}
}
return null;
}
}
回答by Micha? Stochmal
I'd go with setting ObjectMappertimezone as default JVM timezone:
我将ObjectMapper时区设置为默认的 JVM 时区:
ObjectMapper objectMapper = new ObjectMapper();
//Set default time zone as JVM timezone due to one day difference between original date and formatted date.
objectMapper.setTimeZone(TimeZone.getDefault());
It's a better solution if you don't know what timezone is used on a server environment.
如果您不知道服务器环境中使用的是哪个时区,这是一个更好的解决方案。

