aspose file tools
The moose likes JDBC and the fly likes What's wrong with Date data from Oracle 9i? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "What Watch "What New topic
Author

What's wrong with Date data from Oracle 9i?

Yongping Wang
Ranch Hand

Joined: May 15, 2001
Posts: 40
Hello,
A data type is defined as DATE. I use Oracle Enterprise Manager Console to do query and get a correct date like "12-Jan-2005 05:46:46 PM". But the date is displayed as "2005-1-12" from resultset.getDate(col) (thin driver) and "2005-1-12 17:46:46.0" from x.getString(col).
I don't get it. What happened to the date data?
Thanks in advance.

Y.P
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


x.getString(col).

You shouldn't be accessing a date as a String. Use a Date object. And look at java.text.SimpleDateFormat - it should give you a hint as to what you need to be doing.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Yongping Wang
Ranch Hand

Joined: May 15, 2001
Posts: 40
[You shouldn't be accessing a date as a String.]
That's what I was thinking but wrong. I am not sure if you noticed the hours, minutes and seconds are chopped off if I use getDate(). It seems like Oracle thin JDBC driver treats Date quite differently (compared to MS SQL Server).

Y.P
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


I am not sure if you noticed the hours, minutes and seconds are chopped


No, I did notice. A java.sql.Date object is really a long value (the number of milliseconds between the date and January 1, 1970, 00:00:00 GMT.) So assuming you always handle a Date object as a Date object its value will remain constant. However, when you display a date, unless you format it as you wan't to see it, all you are doing is calling the default toString() method on that object. This method returns the date as a String formatted in the "date escape format" i.e. yyyy-mm-dd. It doesn't change the value of the long number which represents the date.
Yongping Wang
Ranch Hand

Joined: May 15, 2001
Posts: 40
It seems not true.
OK. now I use a SimpleDateFormat (using a pattern like "MM-dd-yyyy HH:mm:ss") to format the Date object (getDate(col)). Unfortunately I am still unable to get the hours, minutes, and seconds (all these values are 00 while its real value is non-zero).
Thanks for your reply.
Y.P
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Woops! I should read the JavaDocs before answering
(From the JavaDocs for java.sql.Date: )

To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

[ January 26, 2005: Message edited by: Paul Sturrock ]
Yongping Wang
Ranch Hand

Joined: May 15, 2001
Posts: 40
Obviously getDate() doesn't work for me. Instead getTimestamp() is a quick and reliable method to resort. It doesn't need any format conversion.
Thanks for your posts again.
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1112

Well done Yongping!
You finally found the right answer -- even though you were misled by Paul.
Yes, using "getTimestamp()" is the way to go.
But in any case, thanks for the information regarding the "getString()" method. I didn't know about that

Good Luck,
Avi.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You are welcome Yongping Wang. Apologies again for initially sending you down the wrong route.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: What's wrong with Date data from Oracle 9i?
 
Similar Threads
Convert a String with a date valus for Oracle
Server started with Deployment Errors
How to handle this Date format?
XSL distinct values
An SQLException has occuredjava.sql.SQLException: Stream has already been closed