Jan 7, 2009
Oracle, Timestamps and Timezones
I recently had a problem with retrieveing timestamps from an Oracle table (you might remember my previous post on Dates and Timestamps), where I would get an error such as this:
java.lang.IllegalArgumentException at sun.util.calendar.ZoneInfo.getOffset(ZoneInfo.java:358) at oracle.jdbc.driver.DateTimeCommonAccessor.zoneOffset(DateTimeCommonAccessor.java:433) at oracle.jdbc.driver.DateTimeCommonAccessor.getMillis(DateTimeCommonAccessor.java:471) at oracle.jdbc.driver.DateTimeCommonAccessor.getDate(DateTimeCommonAccessor.java:105) at oracle.jdbc.driver.OracleResultSetImpl.getDate(OracleResultSetImpl.java:737) at oracle.jdbc.driver.OracleResultSet.getDate(OracleResultSet.java:1637) at org.apache.commons.dbcp.DelegatingResultSet.getDate(DelegatingResultSet.java:256)
This was being caused due to a couple of factors:
- The data had been inserted using a GregorianCalendar set to the GMT timezone.
- Oracle has no timezone setting by default.
- I originally just assumed I could select the Date from the table with ResultSet.getDate().
It wasn’t until I investigated the problem thoroughly, did I discover the first point, which then made the second point become clear in perspective. Because the date was inserted with a GregorianCalendar set to GMT, I had to extract it the same way.
Thus, if you have Date or Timestamp columns that were set for a particular timezone, you need to extract them with the same timezone.
GregorianCalendar m_cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
...
// in your code that selects the information
Date date = new Date( rs.getTimestamp("EVENT_DATE", m_cal ).getTime() );
This should solve the error.