• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

converting sqldates?

 
kelvin cheung
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the date-format 12.12.2004 in Oracle.
But when I read into ResultSet in Java,
I get 2004-12-12.
How can I get the same format as in Oracle?

I tried this :

which gave me this correct data :

TK302 2004-12-12

TP329 2904-12-09



when i tried this :

then the date is incorrect, dont know how those came there:

TK302 Sat Jun 26 00:12:00 CET 0017

TP329 Sun Dec 13 00:12:00 CET 0016


what can i do?

regards,
kelvin.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You recover that Date as a Date object, then turn it into a string and parse the string back into a date. Why not just handle it as a Date?

A point to note - which is often repeated in these forums - is that databases and Java don't handle dates as formatted strings (e.g. 12-03-2005). They are longs.
 
kelvin cheung
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

because i wanted to use the method : formatter.parse().
which takes a String in as parameter.

hmmm ..?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why? You have a Date, which you convert to a string just so you can parse it back into a Date?
 
kelvin cheung
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i need the date.
so i store it in to the class that i have made (VO).
then put the object into an arraylist.
then put to a textfield in GUI.

thanks ^^
 
kelvin cheung
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.

i get this error message :

INSERT INTO student_exam VALUES(102,'JP329', '2904-11-09')
ORA-01861: literal does not match format string

the date above is a String.
i need format to be '09.11.2904' as a sqlDate.
hm.. ?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use a PreparedStatement and bind the date object you don't need to consider formatting at all. Date objects and date data types have no inherent format. They only get formatted when you convert them to strings. For example the date 1st Jan 1970 00:00:00 is the value 0. Now I could format it as "1/1/70", "1st January 1970 00:00:00", "1970-01-01" etc., but its value remains 0.

Have a look at java.sql.PreparedStatement and search through this forum for examples - there are countless other people who have posted here with the same issue.
[ February 28, 2005: Message edited by: Paul Sturrock ]
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kevin

alternatively you can try this



thanks
[ March 01, 2005: Message edited by: Shailesh Chandra ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic