简体   繁体   English

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

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

This is my Base Class for Entities with audit fields.这是我的带有审计字段的实体的基类。 For fields @CreatedDate, @LastModifiedDate, by default it is saving my system time.对于字段@CreatedDate、@LastModifiedDate,默认情况下它会节省我的系统时间。 My requirement is to save timestamp in UTC.我的要求是以UTC格式保存时间戳。

Does anyone have a solution of this?有没有人有这个解决方案?

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;

}

This is problem of time zone.这是时区问题。 use this code for spring boot.将此代码用于spring boot。

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

暂无
暂无

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

相关问题 Spring 启动 - 实体:如何添加 CreatedDate(UTC 时区感知)、LastModifiedDate(UTC 时区感知)、CreatedBy、LastModifiedBy - Spring boot - Entity : How to add CreatedDate (UTC timezone aware), LastModifiedDate (UTC timezone aware), CreatedBy, LastModifiedBy 使用 Spring 数据 JPA @LastModifiedDate 从审计中排除某些字段 - Exclude some fields from audit with Spring Data JPA @LastModifiedDate Spring审核-Spring如何自动将@CreatedDate和@LastModifiedDate插入数据库? - Spring Auditing - How does spring insert @CreatedDate and @LastModifiedDate into database automaticaly? Spring Data不通过@CreatedDate填充@LastModifiedDate - Spring Data fills @LastModifiedDate by not @CreatedDate spring jpa auditing lastmodifiedby和lastmodifiedDate都可以,但createdBy和createdDate注释总是为null - spring jpa auditing lastmodifiedby and lastmodifiedDate are ok, but createdBy and createdDate annotation are always null 在 Spring 数据 JDBC 中绕过 @CreatedDate 和 @LastModifiedDate - In Spring Data JDBC bypass @CreatedDate and @LastModifiedDate 如何为 Spring Boot JPA 时间戳指定 UTC 时区 - How to specify UTC timezone for Spring Boot JPA Timestamp Spring JPA Hibernate 自动填充审计字段(创建 ID/时间戳等) - Spring JPA Hibernate Auto Populate Audit Fields (Create ID / Timestamp etc) mongodb 在 spring boot 中审计以保存 createdDate、lastModifiedDate、createdBy、lastModifiedBy - mongodb auditing in spring boot for saving createdDate, lastModifiedDate, createdBy, lastModifiedBy 如何生成@CreatedDate LocalDateTime 作为时间戳? - How to generate a @CreatedDate LocalDateTime as Timestamp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM