Convert Microsoft Json Date To Java Date

时光怂恿深爱的人放手 提交于 2019-12-12 03:22:19

问题


I consume a rest service and i get a json object , and i map json object to my java object with Gson library. But Date Json with following format not deserialized :

 "/Date(1466606168687+0430)/"

I also checked following gson object ,but json date is not deserialized yet:

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssz").create();

Update: My problem is time zon for deserializing json date with timezon.


回答1:


finally i use this code for date deserialization :

public class JsonDateDeserializer implements com.google.gson.JsonDeserializer<Date>{

public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

    String s = json.getAsJsonPrimitive().getAsString();

    String s1 = s.substring(6, s.length() - 2);

    String[] saa = s1.split("\\+");

    long l = Long.parseLong(saa[0]);
    Date d = new Date(l);
    return d;
}
}


来源:https://stackoverflow.com/questions/38567128/convert-microsoft-json-date-to-java-date

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!