Serialize and Deserialize Joda Time in Gson
register a TypeAdapter
with GSON
to wrap the use of a Joda preconfigured Formatters :
public static Gson gsonDateTime() { Gson gson = new GsonBuilder() .registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() { @Override public JsonElement serialize(DateTime json, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(ISODateTimeFormat.dateTime().print(json)); } }) .registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() { @Override public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { DateTime dt = ISODateTimeFormat.dateTime().parseDateTime(json.getAsString()); return dt; } }) .create(); return gson; }