Spring Boot version 2.3.1
.
I have the following class:
@Data
@Entity
@NoArgsConstructor
public class CarParkEvent implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Enumerated(EnumType.STRING)
private EventType eventType;
@Column(columnDefinition = "TIMESTAMP")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createdAt;
After storing the event locally I have to send it to the backend. For now, sending data looks like:
"createdAt" : "2020-10-01T17:15:23.481"
However, backend expects the following data format:
"createdAt" : "2020-10-01T17:15:23Z"
The main idea of the application is to send events to the backend.
So I need to send exact data which they are expecting.
After looking at the following answers:
- What is this date format? 2011-08-12T20:17:46.384Z
- How to convert LocalDateTime to
“yyyy-MM-dd'T'HH:mm:ss'Z'”
format
Could not understand how exactly I have to store this field locally?
Do I need to shift to OffsetDateTime
or ZonedDateTime
?
Also, I want to use Java 8 Date Time API.
For controlling date time setting use following utility class:
@Slf4j
@UtilityClass
public class TimeClock {
private LocalDateTime dateTime;
public LocalDateTime getCurrentDateTime() {
return (dateTime == null ? LocalDateTime.now() : dateTime);
}
public void setDateTime(LocalDateTime date) {
log.info("Set current date for application to: {}", date);
TimeClock.dateTime = date;
}
public void resetDateTime() {
log.info("Reset date for the application");
TimeClock.dateTime = LocalDateTime.now();
}
/**
* Different formats for current dateTime.
*/
public LocalDate getCurrentDate() {
return getCurrentDateTime().toLocalDate();
}
public LocalTime getCurrentTime() {
return getCurrentDateTime().toLocalTime();
}
}
I use it anywhere where the new date-time should be created.
What is the best strategy for storing ISO 8601 data time format with Java 8 and Spring Boot?