Since your POJO has several Date fields, and incoming JSON have these dates in different formats, you need to write your own deserializer for Date that can handle these formats.
class DateDeserializer implements JsonDeserializer<Date> { @Override public Date deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException { String myDate = je.getAsString();
You can register this as a type adapter when creating a Gson instance:
Gson gson = new GsonBuilder() .registerTypeAdapter(Date.class, new DateDeserializer()) .create();
You can, of course, just write your own deserializer for your POJO and populate everything yourself from the parse tree.
Another option would be to simply set them as String in your POJO, and then get the getters for each field to convert them to Date .
In addition, if you are not completely attached to using Gson, the Jackson JSON parser (by default) uses your POJO setters during deserialization, which will give you explicit control over the setting of each field.
source share