簡體   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