如何让 dbUnit 或 hibernate 停止从我的日期中减去一个小时?
因此,我有一个 XML 文件,将 dbUnit 用作数据源,其中包含一些日期。文件中的日期如下所示:“2010-02-04”,但是一旦加载并且我访问它以在测试中打印或比较,它认为日期是“2010-02-03 23:00”。
我猜这与 EDT/EST 有关,但我不确定如何强制 dbUnit(或可能休眠?)使用正确的时区。
有人对 dbUnit 和日期有任何经验吗?
谢谢, 彼得
编辑
好吧,我很确定,由于某种原因,它读取日期为 EDT,这是正确的,然后将其存储为 EST,这在技术上是不正确的(我们这里是 EDT),但根据我的计算机显示是正确的这是美国东部时间。这种转换导致日期“损失”了一个小时。不知道为什么 Java 认为我们是 EST(Windows XP 知道我们不是),而且我双重不确定为什么 dbUnit 认为日期应该是 EDT,因为它像 Java 一样,可能应该将错误的时区读取为 EST。这很令人困惑。
编辑 我删除了 Hibernate XML,因为这不是问题所在。经过更多探索后,发生了以下情况: 1. dbUnit 从源 XML 中读取字符串形式的日期,并将其转换为 java.sql.Date。从本质上讲,它们不存储小时/分钟/秒,但是当您查看毫秒时,它显然会调整 4 小时,以表明虽然在 EDT 中是午夜,但在 UTC 中却是凌晨 4 点。此外,这还有日历的支持,日历的时区标记为“美国/纽约”或类似的内容。我记不太清了。 2. 每当我打印这个日期时,我的系统(认为是美国东部标准时间)会重新将世界标准时间凌晨 4 点重新转换为前一天晚上 11 点,这是正确的,除非我们不在美国东部时间。 3. 每当我自己创建一个日期进行测试作为示例时,由于我的 JVM 认为我们处于 EST,它会添加 5 小时的毫秒数来区分它和UTC。显然,这些是不同的“日期”,而且它失败了。
所以,我想真正的问题是双重的: 1. 为什么我的主机认为是 EST?据我所知,我的 Windows XP 设置是正确的。我知道 Java 1.4 中曾经存在一个问题,导致 TZ 错误,但我认为此问题已得到修复,并且我正在运行(据说)1.6。 2. 为什么 dbUnit 在同一个 JVM 中使用正确的奇特 TZ。
我想我可以停止使用日期,但我不一定有这种奢侈。
So, I have an XML file I am using with dbUnit as a data source, and it has some dates in it. The date in the file is like so: "2010-02-04", but once it gets loaded and I access it to print or compare in tests, it thinks the date is "2010-02-03 23:00".
I'm guessing this has to do with EDT/EST, but I'm not sure how I can force dbUnit (or possibly hibernate?) to use the correct timezone.
Does anyone have any experience with dbUnit and dates?
Thanks,
Peter
EDIT
Okay, I'm pretty sure, for some reason, it's reading the date in as EDT, which is correct, then storing it as EST, which is not technically correct (we are EDT here), but is correct per my computer which says it's EST. This conversion is causing the date to "lose" an hour. Not sure why Java thinks we are EST (Windows XP knows we're not), and am doubly unsure why dbUnit thinks the date should be EDT, since it, like Java, should probably be reading the incorrect timezone as EST. This is pretty confusing.
EDIT
I took the Hibernate XML out because that's not the problem. After more poking around, here is what is happening:
1. dbUnit reads the date as a String from the source XML and converts it to a java.sql.Date. By nature, these do not store hours/minutes/seconds, but when you look at the millis, it's clearly adjusted by 4 hours worth in order to show that, though it's midnight here in EDT, it's 4am in UTC. Also, this is backed by a Calendar, which has a timezone that is labeled "America/New York" or something equivalent. I can't exactly remember.
2. Whenever I print this date, my system, which thinks it's EST reconverts 4am UTC to 11pm the previous day, which would be correct, except we're not in EST.
3. Whenever I create a date myself for testing as the example, since my JVM believe we're in EST, it adds five hours worth of millis to make the difference between it and UTC. Clearly, these are different 'dates', and it fails.
So, the real question is, twofold, I guess:
1. Why does my console think it's EST? My Windows XP settings are correct as far as I know. I understand there used to be a problem in Java 1.4 that got the wrong TZ, but I think this has been since fixed and I am running (supposedly) 1.6.
2. Why does dbUnit, which is in the same JVM using the proper fancy TZ.
I suppose I could stop using dates all together, but I don't necessarily have that luxury.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
5年后,我希望我能正确理解你的问题,因为你写了很多。尽管如此,我也遇到了类似的问题。
即使我在 Spring 应用程序中设置了 TimeZone.setDefault(TimeZone.getTimeZone("UTC")); ,DbUnit 或 Hibernate 仍将我的 UTC 时间切换为本地时间。
当我使用
-Duser.timezone=EDT
时,正如 Andy 指出的那样,我的测试通过了。因此,我假设 DbUnit 在实际应用程序启动之前填充其数据库,因此默认时区设置得太晚。为了避免这种情况,我为 UTC 想出了以下两个解决方法,对于 ETC(或任何其他时区)只需相应地切换即可。
常规应用
所有您的测试类都必须设置默认时区。为了不重复我自己,我让所有测试扩展
UtcTimeZoneTest
。Spring应用程序
在Spring应用程序中你也可以做同样的事情,但我认为下面的更好一些。同样,所有您的测试类都必须执行此操作。 (虽然我不是 Spring 专家,但我希望这没有缺点。)
5 years later, I hope I understand your problem correctly because you wrote quite a lot. Nevertheless I had a similar problem.
DbUnit or Hibernate switched my UTC times to my local time even though I had set
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
in my Spring application.When I used
-Duser.timezone=EDT
, as Andy pointed out, my tests passed. Due to this I assumed that DbUnit fills its database before the actual application is started and therefore the default timezone is set too late.To avoid this I came up with the two following workarounds for UTC, for ETC (or any other timezone) just switch it accordingly.
Regular application
All your test classes must set the default time zone. To not repeat my self I let all my test extend
UtcTimeZoneTest
.Spring application
In Spring applications you can also do the same thing, but I think the following is a bit nicer. Again all your test classes, must do this. (Although I am not a Spring expert and I hope this has no drawbacks.)
您如何执行测试?尝试将以下 VM 参数添加到命令行(或者无论您如何执行测试):
How are you executing your tests? Try adding the following VM argument to the command line (or however you're executing your tests):