3

I am using Jackson to serialize POJOs to JSON, but am having difficulty with the formatting of dates. I have come across this wiki on Jackson's website: http://wiki.fasterxml.com/JacksonFAQDateHandling

Via this article, I did the following:

objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);

This prints Date objects in ISO-8601 format, which is what I want. Except for the "+0000". According to the ISO-8601 format, the +hhmm is the time offset from UTC. Is there a way in Jackson to disable the time offset? Or will I just have to specify a custom date format?

user972276
  • 2,973
  • 9
  • 33
  • 46

1 Answers1

4

I think your best bet is to just pass in a new date format when serializing. For example:

final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm"));

Based on the JavaDocs on github which gives some insight into how date formatting is determined, without specifying your own, I think you can only either use numeric timestamps or the built in ISO format that Jackson defines. Docs for reference:

/**
     * Feature that determines whether Date (and date/time) values
     * (and Date-based things like {@link java.util.Calendar}s) are to be
     * serialized as numeric timestamps (true; the default),
     * or as something else (usually textual representation).
     * If textual representation is used, the actual format is
     * one returned by a call to
     * {@link com.fasterxml.jackson.databind.SerializationConfig#getDateFormat}:
     * the default setting being {@link com.fasterxml.jackson.databind.util.StdDateFormat},
     * which corresponds to format String of "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
     * (see {@link java.text.DateFormat} for details of format Strings).
     *<p>
     * Note: whether this feature affects handling of other date-related
     * types depend on handlers of those types, although ideally they
     * should use this feature
     *<p>
     * Note: whether {@link java.util.Map} keys are serialized as Strings
     * or not is controlled using {@link #WRITE_DATE_KEYS_AS_TIMESTAMPS}.
     *<p>
     * Feature is enabled by default, so that date/time are by default
     * serialized as timestamps.
     */
    WRITE_DATES_AS_TIMESTAMPS(true),
Nick DeFazio
  • 2,412
  • 27
  • 31
  • I was afraid this would be my only option, but I just wanted to make sure as I am not very familiar with Jackson. – user972276 Nov 11 '15 at 20:51