我正在开发一个比赛计时系统,在某些情况下,我需要从 H2DB 中检索一个时间对象。像它的兄弟(或姐妹)一样,时间数据类型是相对于 1970 年 1 月 1 日,并以 SQL 中的“hh:mm:ss”格式表示,日期默认设置为 01-01-1970。默认情况下,它映射到“java.sql.Time”对象。作为一个值得信赖的学徒,我编写了以下代码以将小时与分钟分开以用于显示目的。
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 的输出。
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”的值我做错了什么?先感谢您)。
编辑按要求,这是我的数据库代码:
表创建语句:
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 对象