Retrofit error 415 400 object with Date field Fix @JsonFormat annotation

Retrofit error 415 or error 400 on an object with a Date field can be fixed by using @JsonFormat annotation with the proper date pattern format specified in the annotation.

java.util.Date toString() (for my locale/region) returns a string date format of MMM dd, yyyy hh:mm:ss aa

Example: Oct 22, 2015 11:09:55 PM

For Retrofit / Jackson to serialize / deserialize a date (java.util.Date) properly, you must annotate your Date fields with the corresponding date pattern:

@JsonFormat(shape= JsonFormat.Shape.STRING, pattern = "MMM dd, yyyy hh:mm:ss aa")

Pay special attention to the uppercase / lowercase lettering of each date part.  Two digit date of month must be LOWERCASE (i.e. “dd” not “DD”).  Otherwise it will be interpreted as a date of year which can be three digits.  (I was getting bizarre JSON serialization JSON deserialization results such as Oct 22 becoming Dec 362 by having the wrong case on my date part of the date format (i.e. “DD”.)

For the full list of date format parts that Jackson (fasterxml) uses see:

http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html


Posted

in

, ,

by

Tags: