简体   繁体   English

objectmapper.readvalue自动更改时区

[英]objectmapper.readvalue automatically change the timezone

I am sending date from front end as below format as string. 我从前端发送日期,如下格式的字符串。

[{"StartDate":"2018-03-09"}] [{“ StartDate”:“ 2018-03-09”}]

In spring i am using ObjectMapper to get this in POJO. 在春季,我正在使用ObjectMapper在POJO中获得此功能。 While using ObjectMapper.readValue is converting to below date. 使用ObjectMapper.readValue时将转换为以下日期。

Fri Mar 09 05:30:00 IST 2018 星期五2018年3月9日05:30:00

I already tried below code. 我已经尝试过下面的代码。

ObjectMapper objectMapper = new ObjectMapper().setTimeZone(TimeZone.getTimeZone("UTC"));

But its not working for me. 但是它对我不起作用。 Still getting Fri Mar 09 05:30:00 IST 2018 as output. 仍将IST 2018年5月9日星期五05:30:00作为输出。

How to make code independent of timezone. 如何使代码独立于时区。

You are mapping string date to Date object. 您正在将字符串日期映射到Date对象。 You can't have date object without timezone. 没有时区的日期对象是不可能的。 The UTC timezone is being applied correctly by mapper and date is getting stored as 2018-03-09 00:00:00 UTC and for IST that becomes 2018-03-09 05:30:00 IST . 映射器正确地应用了UTC时区,并且日期存储为2018-03-09 00:00:00 UTC 2018-03-09 05:30:00 IST 2018-03-09 00:00:00 UTC并且IST成为2018-03-09 05:30:00 IST If you want 2018-03-09 00:00:00 IST time, instead of UTC , pass IST . 如果您想要2018-03-09 00:00:00 IST时间2018-03-09 00:00:00 IST而不是UTC ,请传递IST

If you are looking to send out date without timezone, you can do that with annotation as shown below (requires json-jackson version >= 2.0) 如果您希望发送不带时区的日期,则可以使用如下所示的注释进行操作(要求json-jackson版本> = 2.0)

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone="Asia/Kolkata")
private Date date;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM