SQL + java, passing time AND date to a field in database

boran

Golden Member
Jun 17, 2001
1,526
0
76
As you might know ms access stores it's date as a date + time, so does java, escept sql does not seem to support this (when writing data from java to access one must use SQL statements) because if I write a time value, only the time gets set, date gets set to 1970 (zero digits for that value) and when I write a date value only the date gets set, and at midnight, (time all zero digits)

am I forced to split this up in two fields ?, because java stores the time as a millisecond value since 1970 this would greatly hinder my way to calculate the passed time.

a tought crossed my mind however, is it possible to perform two sets, one set date, and another set time, will those two overwite eachother ?

if not i'll just have to rewrite my functions to supply a calendar argument.

thanks in advance.
 

boran

Golden Member
Jun 17, 2001
1,526
0
76
well, I triecked it a bit, I used two fields, one for date another for time, strangely when making a java.util.Date object from these two objects I get, a java.sql.Date + a java.sql.Time value in long format I loose one hour, the Date and time contain the right values, it's just when I count em together that I losoe an hour.
 

boran

Golden Member
Jun 17, 2001
1,526
0
76
hmmh, I found out what my exact problem is. I'll walk trough the steps so you can see it:

I get a java.sql.Time object out of the DB, it has a correct time;
I get a java.sql.Date object out of the DB it has a correct time;
I make a java.util.Date object out of these two by this means:
java.util.Date returnDate = new java.util.Date( timeObject.getTime() + dateObject.getTime() )
which gives me one hour short if i'm in GMT+1 and 5 hrs too much when in GMT-5 (my US buddy)
now I replaced it by:
java.util.Date returnDate = new java.util.Date( timeObject.getTime() + dateObject.getTime() + (timeObject.getTimezoneOffset() * -60000) )

which gives me the correct time, but getTimezoneOffset is a deprecated function, so i'd have to put it into a Calendar to have it function correctly etc etc, one bigg pile of a mess imho, is there no other way ? checking the locale or so ?

thanks in advance.
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
Try using java.sql.Timestamp class. It subclasses java.util.Date and most databases store time in that format.
 

boran

Golden Member
Jun 17, 2001
1,526
0
76
the MS access driver sais I use a syntax error in my update statement.

here are some snippets out of my code:

private static String zetTijd = "UPDATE waarden SET timeStamp = ? WHERE naam = 'TimeTest'";
private static PreparedStatement pStZetTijd;

public static void setTijd(Timestamp tijd)
{
try
{
pStZetTijd.setTimestamp(1, tijd);
pStZetTijd.execute();
DbConn.setGestart(true);
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}

and we reeeealy need a code tag here :/ goddamnit

strange tho, for now I'll keep it in seperate values.