0

I am trying to return a model to rest api get call in which the timestamp format is different

    "created_at": {
        "date": {
            "year": 2020,
            "month": "OCTOBER",
            "day": 16,
            "dayOfMonth": 16,
            "dayOfWeek": "FRIDAY",
            "era": "CE",
            "dayOfYear": 290,
            "leapYear": true,
            "monthValue": 10,
            "chronology": {
                "id": "ISO",
                "calendarType": "iso8601"
            }
        },
        "time": {
            "hour": 21,
            "minute": 3,
            "second": 17,
            "nano": 870000000
        },
        "month": "OCTOBER",
        "year": 2020,
        "dayOfMonth": 16,
        "dayOfWeek": "FRIDAY",
        "dayOfYear": 290,
        "monthValue": 10,
        "hour": 21,
        "minute": 3,
        "nano": 870000000,
        "second": 17,
        "chronology": {
            "id": "ISO",
            "calendarType": "iso8601"
        }
    },

I want the timestamp format as "created_at" : "2020-10-06T05:45:14.847Z"

Model format

@Column(name = "CREAT_TS", updatable = false)
@Type(type = Constants.LOCAL_DATE_TIME)
@JsonProperty("created_at")
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
private LocalDateTime createdTimestamp;

I have jackson-datatype-jsr310 in my classpath and registered joda module in objectmapper

  ObjectMapper objectMapper = new ObjectMapper()
                .registerModule(new ParameterNamesModule())
                .registerModule(new JodaModule())
                .registerModule(new Jdk8Module())
                .registerModule(new JavaTimeModule())
                .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
                .setSerializationInclusion(JsonInclude.Include.NON_NULL)
                .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  • 1
    What library are you using for this? --- Looks like it might be Jackson, so did you make sure to add the `jackson-datatype-jsr310` library that contains the code for supporting the `java.time.*` types? Did you remember to register `JavaTimeModule` with the `ObjectMapper`? – Andreas Oct 17 '20 at 11:46
  • 1
    Does this answer you question? [serialize/deserialize java 8 java.time with Jackson JSON mapper](https://stackoverflow.com/q/27952472/5221149) – Andreas Oct 17 '20 at 11:48
  • I have did both which you mentioned still returning the same – Haja Asmath Oct 17 '20 at 14:42
  • Unable to reproduce using Jackson 2.10.3 – Andreas Oct 17 '20 at 15:06
  • *Curious:* If you want the timestamp format as `"created_at" : "2020-10-06T05:45:14.847"`, why did you include a `'Z'` at the end of the format string? – Andreas Oct 17 '20 at 15:07
  • Is the `LocalDateTime` from Java 8 Time API, or is it from the old obsolete Joda-Time library? I could not reproduce using `java.time.LocalDateTime`. – Andreas Oct 17 '20 at 15:10
  • I must have missed the Z "created_at" : "2020-10-06T05:45:14.847Z" – Haja Asmath Oct 17 '20 at 15:13
  • it is java.time.LocalDateTime – Haja Asmath Oct 17 '20 at 15:59

0 Answers0