0

My webservice is giving me this format of Date: 1462575665220. How can I deserialize this using GSON?

I think that I have to use something like this:

new GsonBuilder().setDateFormat("dd-MM-yyyy HH:mm:ss").create();

But I dont know what kind of format is it. Can I deserialize it or I have to change something in the Webservice?

Sorry for bad english, I hope you understand.

Julián
  • 101
  • 1
  • 12
  • Possible duplicate of ["Unparseable date: 1302828677828" trying to deserialize with Gson a millisecond-format date received from server](http://stackoverflow.com/questions/5671373/unparseable-date-1302828677828-trying-to-deserialize-with-gson-a-millisecond) – Chisko May 28 '16 at 16:52

1 Answers1

1

Try this:

GsonBuilder builder = new GsonBuilder(); 

builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
   public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
      return new Date(json.getAsJsonPrimitive().getAsLong()); 
   } 
});

Gson gson = builder.create();
Chisko
  • 3,092
  • 6
  • 27
  • 45