Tuesday, March 17, 2009

Reason #82734 why I hate Oracle JDBC drivers

Are you wondering why the date/time values you read from Oracle 10g's jdbc driver are off by several hours? It's because the 9i/10g drivers don't properly account for the jdbc client's time zone when writing the date value.

So what's the fix?

Suppose you know your server date/time values are stored in GMT. You can call getTimestamp() with a GMT calendar, and then you'll get the right values.

public class OracleJdbcDriverSucks {
  public static Calendar GMT_CALENDAR = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

  public static Date getGMTDate(ResultSet rs, String column) throws SQLException {
    return new Date(rs.getTimestamp(column, GMT_CALENDAR).getTime());

No comments: