I subtract two dates from each other because I need the time difference between them. I toggle the usual problems but when I write it to MySQL (Time) I see there is always 1 hour too much difference. If I expect 2 min difference I get 1 h 2 min. Now I think it’s a timezone problem but is there a way to solve this … ?
Date totalDate = visit.getTotalTime();
Date visitDate = visit.getVisitTime();
java.sql.Time sqlTotalTime = new java.sql.Time(totalDate.getTime() - visitDate.getTime());
ps.setTime(4, sqlTotalTime);
gives 1h 2 sec difference and not 2 sec:
08/Dec/2010:07:27:54
08/Dec/2010:07:27:52
Thank you.
You’re not explaining how exactly you are subtracting the two dates or what “the usual problems” are according to you.
To get the time difference between two dates, you can call
getTime()on both of them to get the number of milliseconds since 01-01-1970 00:00:00 GMT and then subtract these two values:Ofcourse, if you want the difference in seconds, minutes, hours etc. then divide this value by the appropriate factor.
Note that a
java.util.Dateobject does by itself not hold any information about time zones.edit (after your edit) It looks like you are trying to store the time difference in a column of type TIMESTAMP or something similar. Don’t do that; TIMESTAMP etc. are meant for time values, not time difference values. Just store the time difference in a numeric column.