The api I'm currently using returns dates (for the sake of this question, dates and datetimes are the same) in the form of "yyyy-MM-dd" and also in the typical ISO8601 format "2012-06-08T12:27:29.000-04:00"
How do you "cleanly" set up GSON to handle this? Or would my best approach be to treat dates as strings and output in the specific form needed using some custom getter in my model objects?
I'm currently doing the following, but parsing fails whenever I see a "yyyy-MM-dd" field.
return new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
.create();
I'm ultimately using this in an Android + Retrofit context in case there are solutions available via that avenue.
Edit: Per the suggestion below, I created a custom TypeAdapter. My full solution can be seen (as a gist) here: https://gist.github.com/loeschg/2967da6c2029ca215258.