I cannot set properly the date format when using retrofit and trying to read a date like this:
2015-08-29T11:22:09.815479Z
An the GSON converter I'm setting is like this:
GsonConverter gsonConverter = new GsonConverter(
new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSz")
.create()
);
Any clues on what's the problem?
Java Date has millisecond precision so I've been creating the Gson
object like so:
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
.create();
Of course, in a case like this, the microseconds are being truncated whether you put SSS
or SSSSSS
. It is also assumed that the last character of the converted string is always a Z
.
To explain why your pattern didn't work, you used z
(lowercase z) which according to the documentation represents a General Timezone (e.g., Pacific Standard Time; PST; GMT-08:00
). Additionally, if you were to use Z
(uppercase Z), it would also not work since it represents an RFC 822 time zone (e.g., -0800
).
In another scenario where instead of the Z
you have a time zone offset (e.g., -08
, -0800
, or -08:00
), you can use X
, XX
, or XXX
to represent an ISO 8610 time zone. But that requires Java 7 or 8 (I don't think Android is compatible with Java 8 at the moment).
Another method would be to write your own Gson
Serializer and Deserializer; although I haven't tried.
It's also worth looking at java.sql.Timestamp class which is also supported by Gson
and has more fine grained precision (nanoseconds) but it's also an option I haven't explored.