H2DB and Java, an approximate> two hour discrepancy

前端 未结 1 947
暗喜
暗喜 2021-01-22 06:31

I am developing a race timing system, and for several instances, I need to retrieve a time object from H2DB. Like its bretheren (or sisteren), the time data type is relative to

相关标签:
1条回答
  • 2021-01-22 07:11

    java.util.Date is always the UTC time. So, depending of your locale, there will be probably an offset.

    I have heard of Joda Time as a better Java API for dealing with times.

    Anyway, the problem comes from mixing dates treated by java.util.Date and others directly passed as part of the SQL.

    As long as you keep using java.util/sql.Date for everything (and you don't peak inside the DB)(and you don't change the locale) the results will be coherent. The trouble will begin when your SQL tries to pass the values directly as text. So either use Date everywhere or, as trashgod says, everytime you use a Date you take care of setting the Locale to "GMT" (so the internal representation and the output of the Date are the same). Note that this does include Date returned from the DB

    0 讨论(0)
提交回复
热议问题