spring-在Oracle db中插入时间戳后缺少日光调整

时间:2018-08-12 12:42:08

标签: spring spring-boot spring-data-jpa

我正在使用带有hibernate jpa的spring boot将以下类的对象写入oracle db

@Entity
@Table(name="signins")
public class UserSignIn
{
    @Id
    @GenericGenerator(name="signinIdGen" , strategy="increment")
    @GeneratedValue(generator="signinIdGen")
    @Column(name="signin_id",nullable=false)
    private long id;

    @Column(name="timestamp", columnDefinition="timestamp default current_timestamp", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date timestamp;

    @Column(name="username", columnDefinition="varchar(50)", nullable=false)
    private String username; 
}

我使用从Spring Boot jpa扩展CrudRepository的接口来保存和检索对象:

public interface SigninDao extends CrudRepository<UserSignIn, Long> {
}

在插入示例UserSignin并为其设置signin.setTimestamp(somelong)后,我在数据库中看到条目的时间戳为
2018-08-10 15:59:48.667000

问题:当我检索同一条目时,时间戳为-1小时
 "timestamp":"2018-08-10T14:59:48.667+0000"。似乎Date时间戳在插入时将日光调整设置为0。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您插入的时间戳是2018-08-10 15:59 + 0100。您检索的时间戳是2018-08-10 14:59 + 0000。它是完全相同的时间,只是具有两种不同的时区格式。如果您需要不同的格式或不同的输出,则只需要在格式化程序中为日期配置正确的时区(将一些代码张贴在获取时间戳的位置,以及在需要格式化帮助的情况下如何配置JSON格式化程序)

存储时间戳时,它会自动从本地时间转换并存储为UTC时间戳,这也是它的检索结果。因此,存储的时间戳无需记住它是否具有DST,因为转换是在存储它之前发生的。