Showing posts with label jdbctemplate. Show all posts
Showing posts with label jdbctemplate. Show all posts

Friday, October 30, 2009

Tech of the day: I hate time zone!

I have spent the past few hours on one issue: trying to insert a timestamp with time zone properly represented into Oracle through JDBC without success.

I started with using a DATE column in Oracle and a straighforward JdbcTemplate update method with Types.TIMESTAMP passed as the type. A timestamp like 2009-10-29 09:30 GMT+2 is stored as 2009-10-29 00:30, which is correct because my client and Oracle server both runs on PDT. Ideally I would rather store the timestamp in GMT, i.e., 2009-10-29 07:30. Alternatively, it would still be OK if I could store time zone into the database as well, i.e., using Oracle's TIMESTAMP WITH LOCAL TIME ZONE datatype (btw, I can't use TIMESTAMP WITH TIME ZONE if I want to use this column as the partition key!) Unfortunately, JdbcTemplate update for some reason stores it as 2009-10-28 05:30 PDT (i.e., it treated 2009-10-29 00:30 as a GMT time!)

It drove me crazy until I found this article from someone who has the same problem and provided a solution:
The resultSet.s[g]etTimeStamp method has an overloaded cousin, that takes a Calendar parameter.


To use this, you will also need to invoke a different cousin of JdbcTemplate's update which takes a PreparedStatementSetter and override the latter.