• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

How to convert String to Date

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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{
}
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my current date is of the form 17/9/2003 (Oct 17)th.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic