JacksonMapper Date deserialization with UTC time zone fails with given format
up vote
1
down vote
favorite
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 |
up vote
1
down vote
favorite
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 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 at 22:31
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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 at 22:14
quartaela
1,01273164
1,01273164
...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 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 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 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 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 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 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 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 at 22:31
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
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 |
up vote
0
down vote
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 at 22:48
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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 |
up vote
1
down vote
accepted
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 |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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 at 23:10
quartaela
1,01273164
1,01273164
add a comment |
add a comment |
up vote
0
down vote
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 at 22:48
add a comment |
up vote
0
down vote
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 at 22:48
add a comment |
up vote
0
down vote
up vote
0
down vote
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 at 9:14
answered Nov 19 at 22:28
slimane
3944
3944
hey @slimane thanks for the response, however, I am gettingjava.text.ParseException: Unparseable date:
exception.
– quartaela
Nov 19 at 22:48
add a comment |
hey @slimane thanks for the response, however, I am gettingjava.text.ParseException: Unparseable date:
exception.
– quartaela
Nov 19 at 22:48
hey @slimane thanks for the response, however, I am getting
java.text.ParseException: Unparseable date:
exception.– quartaela
Nov 19 at 22:48
hey @slimane thanks for the response, however, I am getting
java.text.ParseException: Unparseable date:
exception.– quartaela
Nov 19 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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 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 at 22:31