0

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.

The problems!

Mathijs Segers
  • 6,168
  • 9
  • 51
  • 75
  • 1
    The digits after the `.` are milliseconds - common in a number of date/time formats. – nnnnnn Feb 26 '16 at 13:12
  • It's [ISO-8601](https://en.wikipedia.org/wiki/ISO_8601), with milliseconds. Very very very common. – dcsohl Feb 26 '16 at 13:18

3 Answers3

1

You can try the following code:

final GsonBuilder builder = new GsonBuilder();
final Gson gson = builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss-'07:00'")
                      .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                      .create();

https://github.com/kreneskyp/openconferenceware-android/blob/master/src/org/osb/GsonFactory.java

Dongsun
  • 134
  • 2
  • Could you explain the -'07:00' part? Anyhow you might want to strip the FieldNaming policy, I don't use Underscores in my API :-). – Mathijs Segers Feb 26 '16 at 13:25
1

The format is ISO_8601

You can use dateformat library to format the date as shown here

Community
  • 1
  • 1
abhilash
  • 5,605
  • 3
  • 36
  • 59
  • Allright I was confused by the milliseconds, I didn't really see those around alot before. That's because I never let Javascript format dates like that. – Mathijs Segers Feb 26 '16 at 13:25
0

So I've been toying in the watcher etc.

I don't know why but it seems like the Z should have been parsed on the front, end not the case.

Currently I got it working by allowing the following format: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" <= note that the Z is encased in single quotes. I left the regular "yyyy-MM-dd'T'HH:mm:ss.SSSZ" as a second guess.

Thanks all for the help.

Mathijs Segers
  • 6,168
  • 9
  • 51
  • 75