Hole wrote:
I've noticed some strange problems in my app...after few hours of
debugging I found out that problems are in java.sql.Timestamp...
Please, if you have spare time, could you look at the following code
and test it with some dates?:
public static Timestamp toSqlTimestamp(String date, String fmt) throws
ParseException, Exception {
Declaring 'throws Exception' is an antipattern.
Timestamp res = null;
try {
SimpleDateFormat f = new SimpleDateFormat(fmt);
res = new Timestamp(f.parse(date).getTime());
} catch(ParseException pexc) {
throw pexc;
This catch clause is redundant since the method already rethrows the
exception without it.
} catch(Exception exc) {
Catching 'Exception' is usually an antipattern.
Failing to log or handle exceptions is an antipattern.
throw exc;
}
return res;
}
This code strangely returns wrong hours, but only whit particular
dates. For example, time from 2009-03-29 02:00:00 to 2009-03-29
03:00:00 (this excluded) returns a timestamp of one hour more...
This is the output of a test program I made:
Original: 2009-03-29 01:45:00
toSqlTimestamp: 2009-03-29 01:45:00
Original: 2009-03-29 02:00:00
toSqlTimestamp: 2009-03-29 03:00:00
Original: 2009-03-29 02:15:00
toSqlTimestamp: 2009-03-29 03:15:00
Original: 2009-03-29 02:30:00
toSqlTimestamp: 2009-03-29 03:30:00
Original: 2009-03-29 02:45:00
toSqlTimestamp: 2009-03-29 03:45:00
Original: 2009-03-29 03:00:00
toSqlTimestamp: 2009-03-29 03:00:00
What time zone is the 'DateFormat' under? I'm guessing that its
Standard Time is one hour after GMT, say in Europe somewhere.
around 2 a.m. on a Sunday morning, a popular time for adjustments.