Alex Rivera | Logout

"Unparseable date: 1302828677828" trying to deserialize with Gson a millisecond-format date received from server

Asked 2011-04-15T00:57:03.550
83

After 4 hours non-stop trying to resolve the problem I have decided to ask here if someone could help me.

The problem is that my Android client when tries to deserialize the data received from a server throw the "Unparseable: 1302828677828" exception.

I would like to know if it is possible to deserialize a millisecond-format date using Gson.

Edit
Report

1 Answer

150

Alfonso's comment:

Finally I got the solution:

// Creates the json object which will manage the information received 
GsonBuilder builder = new GsonBuilder(); 

// Register an adapter to manage the date types as long values 
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();
answered 2012-01-22T11:14:36.757

Your Answer