JacksonMapper Date deserialization with UTC time zone fails with given format












1















I know there are many duplicate questions about the same issue, however, I wasn't able to deserialize given date format into java.util.Date object. The client api I am using returns date fields with 6 digit combined with milliseconds and nanoseconds.




  • 2016-12-08T20:09:05.508883Z

  • 2016-12-08T20:09:05.527Z


Sometimes it includes nano seconds sometimes not. I tried to follow deserialization examples from jackson-databind library itself however couldn't found a workaround. Say this is the example json blob



{
"id": "68e6a28f-ae28-4788-8d4f-5ab4e5e5ae08",
"created_at": "2016-12-08T20:09:05.508883Z",
"done_at": "2016-12-08T20:09:05.527Z"
}


Entity.java



@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class OrderResponse {
private String id;

@JsonProperty("created_at")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'*'", timezone = "UTC")
private Date createdAt;

@JsonProperty("done_at")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'*'", timezone = "UTC")
private Date doneAt;
}


If I only use format yyyy-MM-dd'T'HH:mm:ss jackson mapper deserializes with timezone coming from jvm itself. But I need to use UTC format and I tried also implementing custom deserializer and serializer which doesn't work as well. My question is java.util.Date correct object type? Additionally, I also tried to create my own object mapper with registering new JavaTimeModule() but it didn't work.



Thanks for help.










share|improve this question























  • ...Your problem mostly stems from the fact that you're using java.util.Date, which should be put out of its misery. Are you able to switch to a more appropriate type, like java.time.Instant (although this will require loading an additional jackson module for best support)?

    – Clockwork-Muse
    Nov 19 '18 at 22:29











  • @Clockwork-Muse yeah I can move from Date to Instant however I wasn't able to parse given format to Instant as well.

    – quartaela
    Nov 19 '18 at 22:31
















1















I know there are many duplicate questions about the same issue, however, I wasn't able to deserialize given date format into java.util.Date object. The client api I am using returns date fields with 6 digit combined with milliseconds and nanoseconds.




  • 2016-12-08T20:09:05.508883Z

  • 2016-12-08T20:09:05.527Z


Sometimes it includes nano seconds sometimes not. I tried to follow deserialization examples from jackson-databind library itself however couldn't found a workaround. Say this is the example json blob



{
"id": "68e6a28f-ae28-4788-8d4f-5ab4e5e5ae08",
"created_at": "2016-12-08T20:09:05.508883Z",
"done_at": "2016-12-08T20:09:05.527Z"
}


Entity.java



@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class OrderResponse {
private String id;

@JsonProperty("created_at")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'*'", timezone = "UTC")
private Date createdAt;

@JsonProperty("done_at")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'*'", timezone = "UTC")
private Date doneAt;
}


If I only use format yyyy-MM-dd'T'HH:mm:ss jackson mapper deserializes with timezone coming from jvm itself. But I need to use UTC format and I tried also implementing custom deserializer and serializer which doesn't work as well. My question is java.util.Date correct object type? Additionally, I also tried to create my own object mapper with registering new JavaTimeModule() but it didn't work.



Thanks for help.










share|improve this question























  • ...Your problem mostly stems from the fact that you're using java.util.Date, which should be put out of its misery. Are you able to switch to a more appropriate type, like java.time.Instant (although this will require loading an additional jackson module for best support)?

    – Clockwork-Muse
    Nov 19 '18 at 22:29











  • @Clockwork-Muse yeah I can move from Date to Instant however I wasn't able to parse given format to Instant as well.

    – quartaela
    Nov 19 '18 at 22:31














1












1








1








I know there are many duplicate questions about the same issue, however, I wasn't able to deserialize given date format into java.util.Date object. The client api I am using returns date fields with 6 digit combined with milliseconds and nanoseconds.




  • 2016-12-08T20:09:05.508883Z

  • 2016-12-08T20:09:05.527Z


Sometimes it includes nano seconds sometimes not. I tried to follow deserialization examples from jackson-databind library itself however couldn't found a workaround. Say this is the example json blob



{
"id": "68e6a28f-ae28-4788-8d4f-5ab4e5e5ae08",
"created_at": "2016-12-08T20:09:05.508883Z",
"done_at": "2016-12-08T20:09:05.527Z"
}


Entity.java



@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class OrderResponse {
private String id;

@JsonProperty("created_at")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'*'", timezone = "UTC")
private Date createdAt;

@JsonProperty("done_at")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'*'", timezone = "UTC")
private Date doneAt;
}


If I only use format yyyy-MM-dd'T'HH:mm:ss jackson mapper deserializes with timezone coming from jvm itself. But I need to use UTC format and I tried also implementing custom deserializer and serializer which doesn't work as well. My question is java.util.Date correct object type? Additionally, I also tried to create my own object mapper with registering new JavaTimeModule() but it didn't work.



Thanks for help.










share|improve this question














I know there are many duplicate questions about the same issue, however, I wasn't able to deserialize given date format into java.util.Date object. The client api I am using returns date fields with 6 digit combined with milliseconds and nanoseconds.




  • 2016-12-08T20:09:05.508883Z

  • 2016-12-08T20:09:05.527Z


Sometimes it includes nano seconds sometimes not. I tried to follow deserialization examples from jackson-databind library itself however couldn't found a workaround. Say this is the example json blob



{
"id": "68e6a28f-ae28-4788-8d4f-5ab4e5e5ae08",
"created_at": "2016-12-08T20:09:05.508883Z",
"done_at": "2016-12-08T20:09:05.527Z"
}


Entity.java



@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class OrderResponse {
private String id;

@JsonProperty("created_at")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'*'", timezone = "UTC")
private Date createdAt;

@JsonProperty("done_at")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'*'", timezone = "UTC")
private Date doneAt;
}


If I only use format yyyy-MM-dd'T'HH:mm:ss jackson mapper deserializes with timezone coming from jvm itself. But I need to use UTC format and I tried also implementing custom deserializer and serializer which doesn't work as well. My question is java.util.Date correct object type? Additionally, I also tried to create my own object mapper with registering new JavaTimeModule() but it didn't work.



Thanks for help.







java jackson fasterxml java-date






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 22:14









quartaelaquartaela

1,02283265




1,02283265













  • ...Your problem mostly stems from the fact that you're using java.util.Date, which should be put out of its misery. Are you able to switch to a more appropriate type, like java.time.Instant (although this will require loading an additional jackson module for best support)?

    – Clockwork-Muse
    Nov 19 '18 at 22:29











  • @Clockwork-Muse yeah I can move from Date to Instant however I wasn't able to parse given format to Instant as well.

    – quartaela
    Nov 19 '18 at 22:31



















  • ...Your problem mostly stems from the fact that you're using java.util.Date, which should be put out of its misery. Are you able to switch to a more appropriate type, like java.time.Instant (although this will require loading an additional jackson module for best support)?

    – Clockwork-Muse
    Nov 19 '18 at 22:29











  • @Clockwork-Muse yeah I can move from Date to Instant however I wasn't able to parse given format to Instant as well.

    – quartaela
    Nov 19 '18 at 22:31

















...Your problem mostly stems from the fact that you're using java.util.Date, which should be put out of its misery. Are you able to switch to a more appropriate type, like java.time.Instant (although this will require loading an additional jackson module for best support)?

– Clockwork-Muse
Nov 19 '18 at 22:29





...Your problem mostly stems from the fact that you're using java.util.Date, which should be put out of its misery. Are you able to switch to a more appropriate type, like java.time.Instant (although this will require loading an additional jackson module for best support)?

– Clockwork-Muse
Nov 19 '18 at 22:29













@Clockwork-Muse yeah I can move from Date to Instant however I wasn't able to parse given format to Instant as well.

– quartaela
Nov 19 '18 at 22:31





@Clockwork-Muse yeah I can move from Date to Instant however I wasn't able to parse given format to Instant as well.

– quartaela
Nov 19 '18 at 22:31












2 Answers
2






active

oldest

votes


















1














I found that java.time.format.DateTimeFormatter has ISO_INSTANT format type which supports the format I was looking for.



https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_INSTANT



Basically, I wrote my custom deserializer



public class CustomInstantDeserializer extends JsonDeserializer<Instant> {

private DateTimeFormatter fmt = DateTimeFormatter.ISO_INSTANT.withZone(ZoneOffset.UTC);

@Override
public Instant deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return Instant.from(fmt.parse(p.getText()));
}
}


with @JsonDeserialize annotation on related field.



@JsonProperty("created_at")
@JsonDeserialize(using = CustomInstantDeserializer.class)
private Instant createdAt;





share|improve this answer































    0














    try to use @JsonSetter:



    @JsonSetter("createdAt")
    public Date setCreatedAt(String date){
    SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    sdfDate.setTimeZone(TimeZone.getTimeZone("UTC"));
    return sdfDate.parse(date);
    }


    doneAt should be similar






    share|improve this answer


























    • hey @slimane thanks for the response, however, I am getting java.text.ParseException: Unparseable date: exception.

      – quartaela
      Nov 19 '18 at 22:48











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383409%2fjacksonmapper-date-deserialization-with-utc-time-zone-fails-with-given-format%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I found that java.time.format.DateTimeFormatter has ISO_INSTANT format type which supports the format I was looking for.



    https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_INSTANT



    Basically, I wrote my custom deserializer



    public class CustomInstantDeserializer extends JsonDeserializer<Instant> {

    private DateTimeFormatter fmt = DateTimeFormatter.ISO_INSTANT.withZone(ZoneOffset.UTC);

    @Override
    public Instant deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    return Instant.from(fmt.parse(p.getText()));
    }
    }


    with @JsonDeserialize annotation on related field.



    @JsonProperty("created_at")
    @JsonDeserialize(using = CustomInstantDeserializer.class)
    private Instant createdAt;





    share|improve this answer




























      1














      I found that java.time.format.DateTimeFormatter has ISO_INSTANT format type which supports the format I was looking for.



      https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_INSTANT



      Basically, I wrote my custom deserializer



      public class CustomInstantDeserializer extends JsonDeserializer<Instant> {

      private DateTimeFormatter fmt = DateTimeFormatter.ISO_INSTANT.withZone(ZoneOffset.UTC);

      @Override
      public Instant deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
      return Instant.from(fmt.parse(p.getText()));
      }
      }


      with @JsonDeserialize annotation on related field.



      @JsonProperty("created_at")
      @JsonDeserialize(using = CustomInstantDeserializer.class)
      private Instant createdAt;





      share|improve this answer


























        1












        1








        1







        I found that java.time.format.DateTimeFormatter has ISO_INSTANT format type which supports the format I was looking for.



        https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_INSTANT



        Basically, I wrote my custom deserializer



        public class CustomInstantDeserializer extends JsonDeserializer<Instant> {

        private DateTimeFormatter fmt = DateTimeFormatter.ISO_INSTANT.withZone(ZoneOffset.UTC);

        @Override
        public Instant deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        return Instant.from(fmt.parse(p.getText()));
        }
        }


        with @JsonDeserialize annotation on related field.



        @JsonProperty("created_at")
        @JsonDeserialize(using = CustomInstantDeserializer.class)
        private Instant createdAt;





        share|improve this answer













        I found that java.time.format.DateTimeFormatter has ISO_INSTANT format type which supports the format I was looking for.



        https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_INSTANT



        Basically, I wrote my custom deserializer



        public class CustomInstantDeserializer extends JsonDeserializer<Instant> {

        private DateTimeFormatter fmt = DateTimeFormatter.ISO_INSTANT.withZone(ZoneOffset.UTC);

        @Override
        public Instant deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        return Instant.from(fmt.parse(p.getText()));
        }
        }


        with @JsonDeserialize annotation on related field.



        @JsonProperty("created_at")
        @JsonDeserialize(using = CustomInstantDeserializer.class)
        private Instant createdAt;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 23:10









        quartaelaquartaela

        1,02283265




        1,02283265

























            0














            try to use @JsonSetter:



            @JsonSetter("createdAt")
            public Date setCreatedAt(String date){
            SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
            sdfDate.setTimeZone(TimeZone.getTimeZone("UTC"));
            return sdfDate.parse(date);
            }


            doneAt should be similar






            share|improve this answer


























            • hey @slimane thanks for the response, however, I am getting java.text.ParseException: Unparseable date: exception.

              – quartaela
              Nov 19 '18 at 22:48
















            0














            try to use @JsonSetter:



            @JsonSetter("createdAt")
            public Date setCreatedAt(String date){
            SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
            sdfDate.setTimeZone(TimeZone.getTimeZone("UTC"));
            return sdfDate.parse(date);
            }


            doneAt should be similar






            share|improve this answer


























            • hey @slimane thanks for the response, however, I am getting java.text.ParseException: Unparseable date: exception.

              – quartaela
              Nov 19 '18 at 22:48














            0












            0








            0







            try to use @JsonSetter:



            @JsonSetter("createdAt")
            public Date setCreatedAt(String date){
            SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
            sdfDate.setTimeZone(TimeZone.getTimeZone("UTC"));
            return sdfDate.parse(date);
            }


            doneAt should be similar






            share|improve this answer















            try to use @JsonSetter:



            @JsonSetter("createdAt")
            public Date setCreatedAt(String date){
            SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
            sdfDate.setTimeZone(TimeZone.getTimeZone("UTC"));
            return sdfDate.parse(date);
            }


            doneAt should be similar







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 '18 at 9:14

























            answered Nov 19 '18 at 22:28









            stackerstacker

            1,10925




            1,10925













            • hey @slimane thanks for the response, however, I am getting java.text.ParseException: Unparseable date: exception.

              – quartaela
              Nov 19 '18 at 22:48



















            • hey @slimane thanks for the response, however, I am getting java.text.ParseException: Unparseable date: exception.

              – quartaela
              Nov 19 '18 at 22:48

















            hey @slimane thanks for the response, however, I am getting java.text.ParseException: Unparseable date: exception.

            – quartaela
            Nov 19 '18 at 22:48





            hey @slimane thanks for the response, however, I am getting java.text.ParseException: Unparseable date: exception.

            – quartaela
            Nov 19 '18 at 22:48


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383409%2fjacksonmapper-date-deserialization-with-utc-time-zone-fails-with-given-format%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]