Musings…


Pesky Date data type in Oracle
November 27, 2005, 9:20 am
Filed under: Musings on Tech stuff, Java, Linux and Open Source

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! :-)



No Comments Yet so far
Leave a comment



Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>