Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

292
Views
Jackson deserialize JSON with timestamp field

I have such string:

{
   "debug":"false", 
   "switchTime":"2017-04-12 17:04:42.896026"
}

I'm trying to get object in such approach:

new ObjectMapper().readValue(string, MyObject.class);

And MyObject class:

class MyObject {
    private Boolean debug;
    private Timestamp switchTime;
    //...getters, setters, constructors
}

I have such exception:

com.fasterxml.jackson.databind.exc.InvalidFormatException: 
Can not deserialize value of type java.sql.Timestamp from String
"2017-04-12 17:04:42.896026": not a valid representation (error:
Failed to parse Date value '2017-04-12 17:04:42.896026': 
Can not parse date "2017-04-12 17:04:42.896026Z": while it seems 
to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'', 
parsing fails (leniency? null))  at [Source:   
{"debug":"false", "switchTime":"2017-04-12 17:04:42.896026"}; 

I don't understand why...If i use in debug mode Timestamp.valueOf() with "2017-04-12 17:04:42.896026" - i have success

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

I think you need to set the expected date/time format using @JsonFormat annotation as shown below.

class MyObject {
  private Boolean debug;
  @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
  private Timestamp switchTime;
  //...getters, setters, constructors
}

You can also set timezone as @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS",timezone="PST")

about 4 years ago · Santiago Trujillo Report

0

I faced the similar problem when I was using lombok in the POJO class which has @Builder and @Value annotations

I have also added the annotation AllArgsConstructor and then it is working fine with my custom deserializer code

about 4 years ago · Santiago Trujillo Report

0

The value that you see in debug mode is "toString()" version of actual value of timestamp, so don't rely on what you inspect in debug mode. You can use @JsonFormat annotation that helps you to convert your timestamp with specified format. You need to take care of timezones also while converting!

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!