Solved

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

Posted on 2004-09-28
15
1,502 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:jerra
  • 6
  • 5
  • 4
15 Comments
 
LVL 35

Assisted Solution

by:girionis
girionis earned 100 total points
ID: 12167260
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12167261
You need to use java.util.Date
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12167283
java.util.Date d = rs.getTimestamp("x").getTimestamp();
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 12167289
Sorry

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

Author Comment

by:jerra
ID: 12167296
No it is saved as a date. It is actually sysdate inserted into the column.

>You need to use java.util.Date
How?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12167304
>>How?

See above
0
 
LVL 35

Expert Comment

by:girionis
ID: 12167339
> 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:jerra
ID: 12167380
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12167403
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
 
LVL 35

Expert Comment

by:girionis
ID: 12167414
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
 
LVL 35

Expert Comment

by:girionis
ID: 12167418
You might as well do:

resultSetMetaData.getColumnType(index) == Types.TIMESTAMP
0
 

Author Comment

by:jerra
ID: 12167419
>Is there any type defined for TIMESTAMP in the table?
no the datatype is Date
0
 

Author Comment

by:jerra
ID: 12167424
Thanks for your help. Excellent!
0
 
LVL 35

Expert Comment

by:girionis
ID: 12167426
:)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12167427
8-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

1 Experts available now in Live!

Get 1:1 Help Now