Didn't I put it in the title?! Well, this is my blow-by-blow account of my experience with Java, Linux and other Copyleft stuff. Blogging to take the FUD away!

Sunday, November 27, 2005

Pesky Date data type in Oracle

I was wondering why I was getting the wrong time when I queried a column in an Oracle database with a Date data type. With the help of my friends who are Oracle and Java experts, I have managed to solve the problem.

When saving a Date in an Oracle table with a Date type, you need to save it as a Timestamp. This ensures that the time isn't truncated.


Calendar cal = Calendar.getInstance();

...
//Set Prepared Statement parameter.
ps.setTimestamp(1,new Timestamp(cal.getTimeInMillis()));


To read the date correctly, I used SimpleDateFormat to format the data I got from the table.


SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

...
someBean.set(sdf.format(rs.getTimestamp("DELIVERY_DATE")));


According to the JDBC specifications, the Date data type really doesn't include the time so this is some sort of a workaround.

Whew! :-)

0 Comments:

Post a Comment

<< Home