Note that there are some explanatory texts on larger screens.

plurals
  1. POH2DB and Java, an approximate> two hour discrepancy
    primarykey
    data
    text
    <p>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.</p> <pre><code> if(race.getCutOffTime()!=null){ long cutOffHour=(race.getCutOffTime().getTime())/(3600000); int cutOffMinute=(int)(race.getCutOffTime().getTime()%(60*60*1000)); System.out.println(cutOffHour+":"+cutOffMinute); } </code></pre> <p>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.</p> <pre><code>INSERT INTO MagEye.Races(RaceName, EventID,CutOffTime) VALUES ('TEST', SELECT EventID FROM MagEye.Events WHERE EventName='Sabrina Love',TIME '5:50:00'); </code></pre> <p>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).</p> <p><em><strong>Edit</strong></em> As requested, here is my code for the database:</p> <p>Table Creation Statement:</p> <pre><code>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)); </code></pre> <p>Insertion Statement:</p> <pre><code>INSERT INTO MagEye.Races(RaceName, EventID,CutOffTime) VALUES ('TEST', SELECT EventID FROM MagEye.Events WHERE EventName='Sabrina Love',TIME '5:50:00'); </code></pre> <p>Retrieval:</p> <pre><code>raceDB.result = raceDB.state.executeQuery("SELECT * FROM MagEye.Races WHERE EventID=" + eventID + " ORDER BY RaceName"); java.util.ArrayList&lt;Race&gt; races = new java.util.ArrayList&lt;&gt;(); 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")); </code></pre> <p>Display:</p> <pre><code>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"); } </code></pre> <p><em><strong>Edit</strong></em>: I've decided to replace the Time object with a long primative</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload