如何使用JPA和Hibernate在UTC时区存储日期/时间和时间戳 [英] How to store date/time and timestamps in UTC time zone with JPA and Hibernate

查看:1861
本文介绍了如何使用JPA和Hibernate在UTC时区存储日期/时间和时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何配置JPA / Hibernate将日期/时间存储为数据库中的UTC(GMT)时区?考虑这个带注释的JPA实体:

  public class Event {
@Id
public int id;

@Temporal(TemporalType.TIMESTAMP)
public java.util.Date date;如果日期是2008年2月03日上午9:30太平洋标准时间(美元)

$ / code>

PST),那么我希望2008年2月3日下午5:30的UTC时间存储在数据库中。同样,当从数据库中检索日期时,我希望将其解释为UTC。所以在这种情况下530pm是UTC530。当它显示时,它将被格式化为太平洋标准时间上午9:30。

使用Hibernate 5.2,你现在可以强制UTC时区使用以下配置属性:

 < property name =hibernate.jdbc.time_zonevalue =UTC/> 

欲了解更多详情,请查阅这篇文章


How can I configure JPA/Hibernate to store a date/time in the database as UTC (GMT) time zone? Consider this annotated JPA entity:

public class Event {
    @Id
    public int id;

    @Temporal(TemporalType.TIMESTAMP)
    public java.util.Date date;
}

If the date is 2008-Feb-03 9:30am Pacific Standard Time (PST), then I want the UTC time of 2008-Feb-03 5:30pm stored in the database. Likewise, when the date is retrieved from the database, I want it interpreted as UTC. So in this case 530pm is 530pm UTC. When it's displayed it will be formatted as 9:30am PST.

解决方案

With Hibernate 5.2, you can now force the UTC time zone using the following configuration property:

<property name="hibernate.jdbc.time_zone" value="UTC"/>

For more details, check out this article.

这篇关于如何使用JPA和Hibernate在UTC时区存储日期/时间和时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆