Java Date has precision in milliseconds, so I have a Gson object as follows:
Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") .create();
Of course, in this case, microseconds are truncated, regardless of whether you set SSS or SSSSSS . It is also assumed that the last character of the converted string is always Z
To explain why your pattern doesn't work, you used z (lowercase z), which, according to the documentation, represents the common Pacific Standard Time; PST; GMT-08:00 Pacific Standard Time; PST; GMT-08:00 Pacific Standard Time; PST; GMT-08:00 belt (e.g. Pacific Standard Time; PST; GMT-08:00 ). Also, if you were to use Z (uppercase Z), this would not work either, since it represents the time zone of RFC 822 (e.g. -0800 ).
In another scenario, where instead of Z you have a time zone offset (for example, -08 , -0800 or -08:00 ), you can use X , XX or XXX to represent the ISO 8610 time zone . But this requires Java 7 or 8 (at the moment I do not think that Android is compatible with Java 8).
Another way would be to write your own Gson Serializer and Deserializer ; although I have not tried
It's also worth Gson look at the java.sql.Timestamp class , which is also supported by Gson and has finer precision (nanoseconds), but I have not explored this option yet.
source share