Questo serializzatore/deserializzatore personalizzato può gestire più formati. Si potrebbe provare prima l'analisi in un formato, quindi se fallisce, provare con un secondo formato. Questo dovrebbe anche gestire le date nulle senza esplodere.
public class GsonDateDeSerializer implements JsonDeserializer<Date> {
...
private SimpleDateFormat format1 = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a");
private SimpleDateFormat format2 = new SimpleDateFormat("HH:mm:ss");
...
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
String j = json.getAsJsonPrimitive().getAsString();
return parseDate(j);
} catch (ParseException e) {
throw new JsonParseException(e.getMessage(), e);
}
}
private Date parseDate(String dateString) throws ParseException {
if (dateString != null && dateString.trim().length() > 0) {
try {
return format1.parse(dateString);
} catch (ParseException pe) {
return format2.parse(dateString);
}
} else {
return null;
}
}
}
Spero che ti aiuti, buona fortuna per il tuo progetto.