【发布时间】:2014-06-10 17:35:51
【问题描述】:
我使用的是 Hibernate 4.3.2。我想将我所有的日期存储在数据库中的 UTC 中。我可以在 UTC 中存储日期。现在我正在使用以下方法从数据库中检索日期。
public void getLastActiveTimeAndCheckForIt(int pageId)
{
Criteria criteria = getCurrentSession().createCriteria(MyTable.class, "myTable");
criteria.add(Restrictions.eq("myTable.id", 1));
MyTable row = (MyTable) criteria.uniqueResult();
System.out.println(row.getUpdatedDateTime());
//output : 2014-06-10 03:05:35.0 and actual date in database : 2014-06-10 08:35:35
}
正如我提到的,我在存储在数据库中的输出中得到了不同的日期。我不明白为什么?
我为配置 UTC 时区所做的一些步骤。
1. TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
2. System.setProperty("user.timezone", "UTC");
3. I also set my database timezone in UTC.
【问题讨论】:
-
但是对象包含正确的值?也许失败发生在
System.out.println各自的toString()-日期方法中。 -
嗨@Smutje,谢谢,但是如何调试对象值?
-
在
System.out.println(row.getUpdatedDateTime());设置断点并查看row包含哪些值。 -
嗨,我调试它..在地图中我得到相同的值。
标签: java mysql hibernate datetime timezone