繁体   English   中英

如何在 Spring JPA 中为审计字段 @CreatedDate、@LastModifiedDate 以 UTC 格式保存时间戳

[英]How to save timestamp in UTC format for Audit fields @CreatedDate, @LastModifiedDate in Spring JPA

这是我的带有审计字段的实体的基类。 对于字段@CreatedDate、@LastModifiedDate,默认情况下它会节省我的系统时间。 我的要求是以UTC格式保存时间戳。

有没有人有这个解决方案?

import java.time.LocalDateTime;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;

import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import lombok.Data;


@MappedSuperclass
@Data
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {

    @LastModifiedDate
    @Column(name="last_modified_datetime")
    private LocalDateTime lastModifiedDateTime;

    @CreatedDate
    @Column(name="created_datetime")
    private LocalDateTime createdDateTime;

}

这是时区问题。 将此代码用于spring boot。

@PostConstruct
public void setTimeZone() {
   TimeZone.setDefault(TimeZone.getTimeZone("Etc/UTC"));
}

暂无
暂无

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

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