Julián
Julián

Reputation: 101

Android: How to deserialize this format of Date using GSON?

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.

Upvotes: 0

Views: 263

Answers (1)

Chisko
Chisko

Reputation: 3107

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();

Upvotes: 1

Related Questions