1

I have a Spring Boot API where in JSON request Date field is send but in application code it is not getting formatted correctly and hence DB call is failing due to binding issue

This field is sent in JSON request :-

 "created_at": "2014-08-12 11:48:41.000000"


@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss.SSSSSS", timezone="AEST")
private Date createdAt;

When the getCreatedAt() method is called the Date is coming in format - Tue Aug 12 07:48:41 EDT 2014

But I am expecting it in same format the way it was sent

4
  • Use private String createdAt; instead of private Date createdAt; later convert it into date object. Commented Jan 25, 2019 at 5:07
  • You try by using @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSSSSS") . hope this helps. Commented Jan 25, 2019 at 6:53
  • Hi Aritra, Thank for replying. I tried that also but same result. Commented Jan 25, 2019 at 7:23
  • I've the same issue. could you solve it? Commented Jan 5, 2022 at 19:21

4 Answers 4

1

Add @JsonSerialize(as = Date.class) annotation first and do JsonFormat

@JsonSerialize(as = Date.class)
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss.SSSSSS", timezone="AEST")
private Date createdAt;
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for responding, I have added @JsonSerialize, still I do see same thing.I am getting date in format - Aug 12 07:48:41 EDT 2014 Do I need to add any maven dependency to make these annotations work? Thank you
No need actually .Can you try separate class and try annotate ?
Sorry didn't get you, can you please elaborate what you are saying.
Got you,I tired what you suggested. My code looks like this, POJO class :- @JsonSerialize(using = CustomDateSerializer.class) @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss.SSSSSS", timezone = "AEST") private Date createdAt; public Date getCreatedAt() { System.out.println(createdAt); return createdAt; } public void setCreatedAt(final Date createdAt) { this.createdAt=createdAt; }
And CustomDateSerializer is the same as the link you sent. Still no luck. please assist.
|
0

@JsonFormat annotation is used to specify the format that will be used to serialize the property and seems to be doing it right. when you call getCreatedAt() you will get a Date object and it is up to you how you want to format it.

Comments

0

you can use LocalDateTime like this:

@JsonFormat(shape= JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss.SSSSSS")
@JsonProperty("create_time")
LocalDateTime createTime;

Comments

0
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
    </dependency>

you need this dependency

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.