【问题标题】:How to store DateTime at UTC format with Java 8?如何使用 Java 8 以 UTC 格式存储 DateTime?
【发布时间】:2020-10-01 14:49:12
【问题描述】:

Spring Boot 版本2.3.1.

我有以下课程:

@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;

在本地存储事件后,我必须将其发送到后端。 目前,发送数据如下所示:

“createdAt”:“2020-10-01T17:15:23.481”

但是,后端需要以下数据格式:

“createdAt”:“2020-10-01T17:15:23Z”

应用程序的主要思想是向后端发送事件
所以我需要发送他们期望的确切数据。

看了以下答案后:

无法理解我必须如何在本地存储该字段?

我需要转到OffsetDateTimeZonedDateTime吗?

另外,我想使用 Java 8 日期时间 API

要控制日期时间设置,请使用以下实用程序类:

@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();
    }
}

我在应该创建新日期时间的任何地方都使用它。

使用 Java 8 和 Spring Boot 存储 ISO 8601 数据时间格式的最佳策略是什么?

【问题讨论】:

  • 您使用的是哪个数据库?
  • @Eklavya DB 是 MySql。

标签: spring-boot datetime java-8 date-formatting iso8601


【解决方案1】:

LocalDateTime 的问题在于它根本不存储偏移量或时区,这意味着您不能将其格式化为包含 ZString 用于 UTC 分别为 @987654325 的偏移量@。

我会使用ZonedDateTime,它有一个偏移量和一个区域ID,格式使用特定的DateTimeFormatter(不完全知道你的注释将如何处理这个问题)。

这是一个简单的 Java 小例子:

public static void main(String[] args) {
    // get an example time (the current moment) in UTC
    ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
    // print its toString (implicitly)
    System.out.println(now);
    // format it using a built-in formatter
    System.out.println(now.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
    // or define a formatter yourself and print the ZonedDateTime using that
    System.out.println(now.format(DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssz")));
}

这个小例子的输出是(几秒钟前):

2020-10-01T15:06:16.705916600Z
2020-10-01T15:06:16.7059166Z
2020-10-01T15:06:16Z

我认为你可以在@JSONFormat 注解中使用这样的模式。不完全确定,但现在无法查找。

【讨论】:

  • 在这种情况下,用 JPA 存储 ZonedDateTime 的首选方式是什么?
  • @nazar_art some answer to this question 提到JPA 2.2 增加了对java.time 某些类的支持,并且它有一些链接。不幸的是,ZonedDateTime 似乎不受支持。
  • 看起来应该支持这种情况。
猜你喜欢
  • 2015-12-14
  • 2010-12-21
  • 2020-07-06
  • 2023-04-04
  • 2019-06-04
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
  • 2018-12-20
相关资源
最近更新 更多