File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and Relational Databases and the fly likes How to convert String to Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of RabbitMQ in Depth this week in the Open Source forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "How to convert String to Date" Watch "How to convert String to Date" New topic
Author

How to convert String to Date

Pradeep Selvaraj
Greenhorn

Joined: Sep 29, 2003
Posts: 17
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

Joined: Jul 11, 2001
Posts: 15302
    
    6

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?


GenRocket - Experts at Building Test Data
Pradeep Selvaraj
Greenhorn

Joined: Sep 29, 2003
Posts: 17
my current date is of the form 17/9/2003 (Oct 17)th.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302
    
    6

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

Joined: Sep 29, 2003
Posts: 17
my current date is of the form 17/9/2003 (Oct 17)th.
Pradeep Selvaraj
Greenhorn

Joined: Sep 29, 2003
Posts: 17
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

Joined: Aug 18, 2003
Posts: 218
Can't you use TO_DATE(mydate,'dd/mm/yyyy') in your insert statement? (mydate is the getText() value)
-Sainudheen
Pradeep Selvaraj
Greenhorn

Joined: Sep 29, 2003
Posts: 17
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

Joined: Aug 18, 2003
Posts: 218
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to convert String to Date