I'm currently working on a front-end which serialises dates. I can't seem to find what ISO standard is being used and how to resolve this in Java using GSON.
When I use the default serialisation my Javascript date is formatted to something like 2016-02-26T11:06:36.646Z, what bothers me is the . after the minutes. I'm not sure what format this is, I would expect it to return something like +1:00 or alike.
GSON can't seem to handle this date by default, if it were with the - or + notation it runs fine in my mockMvc test (Spring).
Any guidance which format this is, and how to resolve? I have troubles googling it since the name is not clear
Allright, so I've come further and made a Message converter like this, it's being hit but still can't convert my dateString with the possible options provided (as you can see I've added 4, of which I want the first one to work).
public class ExtendedGsonHttpMessageConverter extends GsonHttpMessageConverter
{
private static final String[] DATE_FORMATS = new String[] {
"yyyy-MM-dd'T'HH:mm:ss.SSSZ",
"yyyy-MM-dd'T'HH:mmZ",
"yyyy-MM-dd'T'HH:mm:ssZ",
"yyyy-MM-dd'T'HH:mm:ss-'07:00'"
};
public ExtendedGsonHttpMessageConverter()
{
super();
super.setGson(buildGson());
}
protected static Gson buildGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
return gsonBuilder.create();
}
private static class DateDeserializer implements JsonDeserializer<Date> {
@Override
public Date deserialize(JsonElement jsonElement, Type typeOF,
JsonDeserializationContext context) throws JsonParseException {
for (String format : DATE_FORMATS) {
try {
return new SimpleDateFormat(format, Locale.GERMANY).parse(jsonElement.getAsString());
} catch (ParseException e) {
}
}
throw new JsonParseException("Unparseable date: \"" + jsonElement.getAsString()
+ "\". Supported formats: " + Arrays.toString(DATE_FORMATS));
}
}
}
The locale Germany is not a necessity.