Oracle/JDBC. SimpleDateFormat on Date column returns time as 00:00:00 (yyyy-MM-dd HH:mm:ss)

Why is it not returning the actual time?

Example:
In Oracle a date column holding: 28-09-2004 10:19:50

Connection connection = CachedConnectionFactory.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select col1 from datetest"); //col1 is a date column
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

simpledateformat.format(date) returns "2004-09-28 00:00:00"


ResultSet resultSet = statement.executeQuery("select to_char(col1,'dd-mm-yyyy hh24:mi:ss') col1 from datetest");
return the string "28-09-2004 10:19:50"

Oracle 9.2.0.2.1
Java 1.3 something
jerraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
girionisCommented:
Check if the date is saved as Timestamp in the database. If it is try to read the column as Timestamp and not as Date:

0
 
CEHJCommented:
You need to use java.util.Date
0
 
CEHJCommented:
java.util.Date d = rs.getTimestamp("x").getTimestamp();
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
CEHJCommented:
Sorry

java.util.Date d = new java.util.Date(rs.getTimestamp("x").getTime());
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
jerraAuthor Commented:
No it is saved as a date. It is actually sysdate inserted into the column.

>You need to use java.util.Date
How?
0
 
CEHJCommented:
>>How?

See above
0
 
girionisCommented:
> No it is saved as a date. It is actually sysdate inserted into the column.

> >You need to use java.util.Date
> How?

You might as well use:

Timestamp date = rs.getTimeStamp("columnname");
simpledateformat.format(date)
0
 
jerraAuthor Commented:
My program dynamically determines the column type from a not specified table.
...
resultSetMetaData.getColumnType(index) == Types.DATE

so instead I have to treat every date column unconditionally as timestamp? awkward...
0
 
CEHJCommented:
Columns are usually able to be treated as Timestamp anyway. You could always do

Object column = rs.getObject(x);
if (columns instanceof Timestamp) {
   //
}
else if (column instanceof java.sql.Date) {
   //
}
0
 
girionisCommented:
Is there any type defined for TIMESTAMP in the table? If yes use that. But if it is a Timestamp datat type in the database then you should be using a Timestamp.
0
 
girionisCommented:
You might as well do:

resultSetMetaData.getColumnType(index) == Types.TIMESTAMP
0
 
jerraAuthor Commented:
>Is there any type defined for TIMESTAMP in the table?
no the datatype is Date
0
 
jerraAuthor Commented:
Thanks for your help. Excellent!
0
 
girionisCommented:
:)
0
 
CEHJCommented:
8-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.