H2DB和Java,近似>两小时的差异 [英] H2DB and Java, an approximate> two hour discrepancy
问题描述
我正在开发一个比赛计时系统,对于几个实例,我需要从H2DB中检索一个时间对象。与它的bretheren(或sisteren)一样,时间数据类型相对于1970年1月1日,并以SQL表示'hh:mm:ss'格式,日期默认设置为01-01-1970。它默认映射到'java.sql.Time'对象。作为一个信任的padawan,为了显示目的,我将以下内容编码为单独的小时数。
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 1st January 1970 and is expressed in SQL in the 'hh:mm:ss' format, with the date being by set by default to 01-01-1970. It is by default mapped to the a 'java.sql.Time' object. Being a trusting padawan, I coded the following to separate hours from minutes for display purposes.
if(race.getCutOffTime()!=null){
long cutOffHour=(race.getCutOffTime().getTime())/(3600000);
int cutOffMinute=(int)(race.getCutOffTime().getTime()%(60*60*1000));
System.out.println(cutOffHour+":"+cutOffMinute);
}
然而,Java的时间处理臭味因为这些函数输出了意外的值,例如,我的数据库中的以下语句给出3:30的输出。
Java's time handling stink arises however in that these functions output unexpected values, for example, the following statement from my db, gives an output of 3:30.
INSERT INTO MagEye.Races(RaceName, EventID,CutOffTime)
VALUES ('TEST', SELECT EventID FROM MagEye.Events
WHERE EventName='Sabrina Love',TIME '5:50:00');
更改此语句以反映'0:0:0'的时间,给出一个值-2:00
我做错了什么?谢谢(提前)。
Changing this statement to reflect a time of '0:0:0', gives me a value of "-2:00" What am I doing wrong? Thank you (in advance).
编辑 根据要求,这是我的数据库代码:
Edit As requested, here is my code for the database:
表创建声明:
CREATE TABLE IF NOT EXISTS MagEye.Races (RaceID INT PRIMARY KEY AUTO_INCREMENT , RaceName VARCHAR(100) ,EventID INT, Description TEXT, MaxEntrants INT, MinAge INT, MaxAge INT, RacePrefix VARCHAR (5), TimingMethod CHAR(1), CutOffTime TIME, RaceEnd TIMESTAMP,Finished BOOLEAN DEFAULT FALSE, Autostart BOOLEAN, FOREIGN KEY(EventID) REFERENCES MagEye.Events(EventID));
插入声明:
INSERT INTO MagEye.Races(RaceName, EventID,CutOffTime) VALUES ('TEST', SELECT EventID FROM MagEye.Events WHERE EventName='Sabrina Love',TIME '5:50:00');
检索:
raceDB.result = raceDB.state.executeQuery("SELECT * FROM MagEye.Races WHERE EventID=" + eventID + " ORDER BY RaceName");
java.util.ArrayList<Race> races = new java.util.ArrayList<>();
while (raceDB.result.next()) {
Race thisRace;
String timingMethodString = raceDB.result.getString("TimingMethod");
Race.TimingMethod timingMethod = null;
if (timingMethodString != null) {
timingMethod = Race.TimingMethod.valueOf(timingMethodString);
} else {
timingMethod = Race.TimingMethod.MANUAL;
}
thisRace = new Race(raceDB.result.getInt("RaceID"), event, raceDB.result.getString("RaceName"), raceDB.result.getString("Description"), raceDB.result.getInt("MaxEntrants"), raceDB.result.getInt("MinAge"), raceDB.result.getInt("MaxAge"), raceDB.result.getString("RacePrefix"), timingMethod,(raceDB.result.getTime("CutOffTime")), raceDB.result.getBoolean("Autostart"));
thisRace = new Race(raceDB.result.getInt("RaceID"), event, raceDB.result.getString("RaceName"), raceDB.result.getString("Description"), raceDB.result.getInt("MaxEntrants"), raceDB.result.getInt("MinAge"), raceDB.result.getInt("MaxAge"), raceDB.result.getString("RacePrefix"), timingMethod,(raceDB.result.getTime("CutOffTime")), raceDB.result.getBoolean("Autostart"));
显示:
if(race.getCutOffTime()!=null){
long cutOffHour=(int)(race.getCutOffTime().getTime())/(3600000);
RacesCutOffLength.setText(cutOffHour+"");
int cutOffMinute=(int)(race.getCutOffTime().getTime()%(60*60*1000));
this.RacesMinutes.setText(cutOffMinute+"");
}
else{
RacesCutOffLength.setText("0");
RacesMinutes.setText("0");
}
修改 :我已经决定用长期主要替换Time对象
Edit: I've decided to replace the Time object with a long primative
推荐答案
java.util.Date
始终 UTC时间。因此,根据您的语言环境,可能会有偏移量。
java.util.Date
is always the UTC time. So, depending of your locale, there will be probably an offset.
我听说过 Joda Time
更好的Java API来处理时间。
I have heard of Joda Time
as a better Java API for dealing with times.
无论如何,问题来自于由 java.util.Date
处理的混合日期以及其他直接传递的日期SQL。
Anyway, the problem comes from mixing dates treated by java.util.Date
and others directly passed as part of the SQL.
只要你继续使用 java.util / sql.Date
就可以了在数据库中达到峰值(并且你不改变语言环境)结果将是连贯的。当您尝试将值直接作为文本传递时,问题将开始。所以要么在任何地方使用日期
,要么就像trashgod所说,每次使用日期
时,你都需要将Locale设置为GMT(因此内部表示和Date的输出是相同的)。请注意,这包括从数据库返回的日期
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
这篇关于H2DB和Java,近似>两小时的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!