0
@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"

vivek sharma
  • 19
  • 1
  • 1
  • 8
  • 1
    Timestamp is a DB type that inherently means 'millis since epoch'. Use a type that actually represents what you want. For example, TemporalType.DATETIME). – rzwitserloot Dec 13 '20 at 06:50
  • 2
    See if you can avoid using `Date`. That class is poorly designed and long outdated. Instead consider `Instant`, `LocalDateTime` or some other class from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 13 '20 at 07:31
  • 2
    Both JPA and Hibernate have been updated to support *java.time* classes. You can stop using `Timestamp`, `Date`, and the other terrible legacy date-time classes. – Basil Bourque Dec 13 '20 at 07:47
  • 3
    *When a new record is saved in the database it saves DateTime like this "2020-12-13 11:41:34.528000000" …* No it doesn’t. It saves in an internal format that we should not care about. It *displays* that format on retrieval. And you’re free to format into any other format your user wants. Which is what you should do. – Ole V.V. Dec 13 '20 at 07:49

1 Answers1

2

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

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36