简体   繁体   English

在 Spring Boot Hibernate for Postgres 中的特定时区中设置 UpdatedTimeStamp

[英]Set UpdatedTimeStamp in a particular TimeZone in Spring Boot Hibernate for Postgres

I have an entity that stores a last updated date like this我有一个存储像这样的上次更新日期的实体

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

This part这部分

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

means that whenever this entity is used, the timestamp is auto updated and persisted on the database.意味着无论何时使用此实体, timestamp都会自动更新并保存在数据库中。

Now my problem is, how do I set this timestamp automatically on a specific TimeZone?现在我的问题是,如何在特定时区上自动设置此timestamp I am using Postgresql.我正在使用 Postgresql。 Any help would be appreciated.任何帮助,将不胜感激。

Avoid using Date , JPA supports the java-8 date classes, so use OffsetDateTime to map TIMESTAMP WITH TIME ZONE避免使用Date ,JPA 支持 java-8 日期类,所以使用OffsetDateTime来映射TIMESTAMP WITH TIME ZONE

Mapping Java 8 Date Types映射 Java 8 日期类型

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

For TIMESTAMP WITHOUT TIME ZONE you can use LocalDateTime对于TIMESTAMP WITHOUT TIME ZONE您可以使用LocalDateTime

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM