Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to convert String to Date

 
Pradeep Selvaraj
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, I jus want to store a string(date) into the ORACLE.I jus get the string(date) from the textfield and when i store it in SQL i get the error invalid month.I will post the code here..
the error is:java.sql.SQLException: ORA-01843: not a valid month
ORA-02063: preceding line from CSE3231B
try
{
SimpleDateFormat sdfInput = new SimpleDateFormat( "dd/mm/yyyy" );
SimpleDateFormat sdfOutput = new SimpleDateFormat ( "dd-mm-yyyy" );
Date date = null;
String s4 = (String) jTextField2.getText();
date = sdfInput.parse(s4);
stmt.executeUpdate(" insert into sales@cse3231b values ('"+s1+"','"+s2+"','"+sdfOutput.format(date)+"')");
}
catch{
}
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the sample date look like that is going in the text field? The only thing I could figure is that you are typing 10-16-2003 in the text field and when Oracle sees 16 as a month, it doesn't like it. So you should be typing 16-10-2003 in the textfield instead. Is this the case?
 
Pradeep Selvaraj
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my current date is of the form 17/9/2003 (Oct 17)th.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so my next logical question is does Oracle take dates formatted in that fasion or must it be mm/dd/yyyy? I don't know enough about Oracle, that is why I am asking.
 
Pradeep Selvaraj
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my current date is of the form 17/9/2003 (Oct 17)th.
 
Pradeep Selvaraj
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U can format the way u give input to ORACLE either in mm/dd/yyyy or dd/mm/yyyy.The problem i face is that i use JAVA to format the date to dd-MON-yyyy. The input date is like dd/mm/yyyy.So i need to convert the date in to ORACLE understandable form
 
Sainudheen Mydeen
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't you use TO_DATE(mydate,'dd/mm/yyyy') in your insert statement? (mydate is the getText() value)
-Sainudheen
 
Pradeep Selvaraj
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it first (to_date) but was unable to change the format so used the SimpleDateFormat.....ne other suggestion plz
 
Sainudheen Mydeen
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pradeep
If getText() returns 16-10-2003 the use TO_DATE(mydate,'DD-MM-YYYY'). If getText() returns 16-OCT-2003 then use TO_DATE(mydate, 'DD-MON-YYYY') in your insert statement. I don't see any problem with this.
-Sainudheen
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic