• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Yongping Wang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Yongping Wang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome Yongping Wang. Apologies again for initially sending you down the wrong route.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic