JacksonMapper Date deserialization with UTC time zone fails with given format
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
add a comment |
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
...Your problem mostly stems from the fact that you're usingjava.util.Date
, which should be put out of its misery. Are you able to switch to a more appropriate type, likejava.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
add a comment |
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
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
java jackson fasterxml java-date
asked Nov 19 '18 at 22:14
quartaelaquartaela
1,02283265
1,02283265
...Your problem mostly stems from the fact that you're usingjava.util.Date
, which should be put out of its misery. Are you able to switch to a more appropriate type, likejava.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
add a comment |
...Your problem mostly stems from the fact that you're usingjava.util.Date
, which should be put out of its misery. Are you able to switch to a more appropriate type, likejava.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
add a comment |
2 Answers
2
active
oldest
votes
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;
add a comment |
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
hey @slimane thanks for the response, however, I am gettingjava.text.ParseException: Unparseable date:
exception.
– quartaela
Nov 19 '18 at 22:48
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
add a comment |
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;
add a comment |
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;
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;
answered Nov 19 '18 at 23:10
quartaelaquartaela
1,02283265
1,02283265
add a comment |
add a comment |
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
hey @slimane thanks for the response, however, I am gettingjava.text.ParseException: Unparseable date:
exception.
– quartaela
Nov 19 '18 at 22:48
add a comment |
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
hey @slimane thanks for the response, however, I am gettingjava.text.ParseException: Unparseable date:
exception.
– quartaela
Nov 19 '18 at 22:48
add a comment |
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
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
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 gettingjava.text.ParseException: Unparseable date:
exception.
– quartaela
Nov 19 '18 at 22:48
add a comment |
hey @slimane thanks for the response, however, I am gettingjava.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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
...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, likejava.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