简体   繁体   English

如何在对象映射器中将时间戳映射到日期

[英]How to map timestamp to date in Object mapper

Right now I am mapping my timestamp to date using object mapper, but this is turning my date object's date to something like this, if timestamp is 1359657000, then it's date should be 01/31/2013, but it is showing me date as 01/31/1970, so I didn't get it what is the issue with this approach. 现在,我正在使用对象映射器将时间戳映射到日期,但这会将我的日期对象的日期更改为这样,如果时间戳为1359657000,则它的日期应为2013年1月31日,但显示的日期为01 / 1970年3月31日,所以我不知道这种方法的问题是什么。

public class Abcd{ 
   private Date year
 }

And now I get this json 现在我得到这个json

{
"data":
{
"fromDate": 1335810600"
}
}

and when I map it like this from Object Mapper 当我从Object Mapper像这样映射它时

ObjectMapper mapper =new Mapper();
mapper.readValue(data,Abcd.class);

now the value of year is coming as 31st Dec 1970, although it should be coming 31st Dec 2013 现在的年份值是1970年12月31日,尽管应该是2013年12月31日

Not sure if I really understand your issue, but in most of situations you don't want to deal with timestamps: 不知道我是否真的了解您的问题,但是在大多数情况下,您不想处理时间戳记:

mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

Then you can define the format of your Date : 然后,您可以定义Date的格式:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
public Date date;

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

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