vivek sharma
vivek sharma

Reputation: 29

@DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss") is Not Working

@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")
@Column(name = "modify_date")
private Date modifyDate;

When a new record is saved in the database it saves DateTime like this "2020-12-13 11:41:34.528000000" but I want it to save like this "2020-12-13 11:41:34"

Upvotes: 0

Views: 15278

Answers (1)

Michael Gantman
Michael Gantman

Reputation: 7808

There are several problems here. First, in your DB if you defined your column as date or timestamp you do NOT have any control of how the DB internally stores it. You may have control of how it would be represented by a client that reads it from DB. But that is all you need.
Second, Avoid outdated class Date and use java.time package. In your case, you might be interested in LocalDateTime or maybe ZonedDateTime class.
Once you change to appropriate class change the annotation

@DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")

to

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")

For more details see the answer to this question

Upvotes: 3

Related Questions