'How to save a "timestamp" in the database with JPA
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.
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Neo |
