在Hibernate中将日期存储为UTC时间日期吗? [英] Storing dates in Hibernate as UTC time dates?

查看:128
本文介绍了在Hibernate中将日期存储为UTC时间日期吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临一个有趣的问题.在我的本地环境中,我处于PDT时间.当我从前端存储一个UTC日期并且在通过Hibernate将日期存储到MySQL数据库中之前,我先在Java中检查日期,然后再存储它,并且该日期实际上已转换为系统的时区.我真的很好奇为什么即使前端中的日期采用UTC日期格式也是如此,所以我将系统时区从PDT更改为EDT,而Hibernate中的日期最终在下次尝试存储时为EDT时间.那个日期.有什么办法可以更改它,以便在所有环境中,如果日期尚未在UTC时区中,则将其存储为UTC时间?

I am facing an interesting question. On my local environment, I am on PDT time. When I store a UTC Date from my front end and right before I store the Date into my MySQL database through Hibernate, I check the date in Java prior to it being stored and the Date is actually converted into whatever my System's time zone was. I was really curious why this was even though my Date in the front end was formatted in UTC date, so I changed my System time zone from PDT to EDT and the date on in Hibernate ended up being in EDT time next time I tried to store that date. Is there any way I can change it so that in all environments, the Date is stored in as UTC time if it is not already in the UTC time zone?

推荐答案

Hibernate 5.2简化了此任务,如

Hibernate 5.2 simplifies this task as explained in this article.

基本上,您现在可以使用以下配置属性为所有TIMESTAMPTIME列强制采用UTC时区:

Basically, you can now force the UTC time zone for all TIMESTAMP and TIME columns using the following configuration property:

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

这篇关于在Hibernate中将日期存储为UTC时间日期吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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