0

I have an application that already exists and I have to add a column in the database with JPA to save a "Timestamp" (complete date with time) and then make an endpoint to receive this "Timestamp" but I'm new to Java and programming and I'm not sure how to do it. I tried to do like this:

Service:

@Column(name="timeStamp", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
@Temporal(TemporalType.TIMESTAMP)
public Date getTimeStamp() {
    return timeStamp;
}

public void setTimeStamp(Date timeStamp) {
    this.timeStamp = timeStamp;
}

Controller:

@PostMapping(
    value = "/{date}",
    produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<?> testDate(@RequestParam Timestamp date){
    return ResponseEntity.ok().body(VisitantService.getTimeStamp());
}

But it keeps giving error.

Ana Lia
  • 13
  • 4
  • You can use Date data type, suppose your column name is UPDATED_DATETIME, then you have to add a member variable which will be mapped to that column like @Column(name = "UPDATED_DATETIME") private Date updatedDateTime;// generate getter and setter and then you have to set you value in this from where you are going to save your dataobject –  Jan 28 '22 at 13:24
  • You can refer this https://stackoverflow.com/questions/2400955/how-to-store-java-date-to-mysql-datetime-with-jpa –  Jan 28 '22 at 13:34

1 Answers1

0

You can use Instant to capture the date and time. It is compatible with JPA. https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html

Neo
  • 779
  • 5
  • 13