【问题标题】:Set UpdatedTimeStamp in a particular TimeZone in Spring Boot Hibernate for Postgres在 Spring Boot Hibernate for Postgres 中的特定时区中设置 UpdatedTimeStamp
【发布时间】:2020-01-19 16:16:51
【问题描述】:

我有一个像这样存储上次更新日期的实体

@Entity
@Table(name = "counter")
public class Count {
    @Id
    @Column(name = "uniq_id")
    String uniqId;

    @Column(name = "amount")
    int amount;

    @Column(name = "points")
    double points;

    @Column(name = "handshake")
    boolean handshake;

    @UpdateTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "updated_date")
    Date timestamp;

    //getter setters
}

这部分

@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_date")
Date timestamp;

表示每当使用此实体时,timestamp 会自动更新并保留在数据库中。

现在我的问题是,如何在特定时区自动设置 timestamp?我正在使用 Postgresql。任何帮助,将不胜感激。

【问题讨论】:

  • A Date 没有时区,所以这可能是不可能的。 PostgreSQL 中的数据类型是什么?
  • @OleV.V.该列在 Postgresql 中像这样timestamp without time zone 创建

标签: java postgresql hibernate spring-boot


【解决方案1】:

避免使用Date,JPA支持java-8日期类,所以使用OffsetDateTime映射TIMESTAMP WITH TIME ZONE

Mapping Java 8 Date Types

@Column(name = "updated_date", columnDefinition = "TIMESTAMP WITH TIME ZONE")
 private OffsetDateTime offsetDateTime;

对于TIMESTAMP WITHOUT TIME ZONE,您可以使用LocalDateTime

@Column(name = "updated_date", columnDefinition = "TIMESTAMP")
private LocalDateTime localDateTime;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-21
    • 2019-03-02
    • 2021-10-06
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    相关资源
    最近更新 更多