在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 simplifies this task as explained in this article.
基本上,您现在可以使用以下配置属性为所有TIMESTAMP
和TIME
列强制采用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"/>