Sunday 20 October 2019

sql - java.text.parseException: unparseable date 1994-09-09





I am trying to send a date value from my java program into an oracle sql database. But I keep getting the error: java.text.parseexception: unparseable date.



I set the date format as:



SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH);

java.sql.Date date = (java.sql.Date)df.parse(dob_text.getText());


I have set my database with the same date format.
And try to send the date through a prepared statement like so:



ps.setDate(3, date); 


I am entering a date 1994-09-09. That's the correct date format for the one I declared right? Is there something wrong with my java formation code? Has anyone else had this problem? Any help would be much appreciated



Answer



This should work, I corrected 2 errors :




  • First of all, the format should have been yyyy-MM-dd since that's the format of your input.


  • Then, you can not implicitely cast java.util.Date to java.sql.Date, you need to use the java.sql.Date constructor and java.util.Date#getTime(). See here








Solution



SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
java.sql.Date SQLDate = new java.sql.Date(df.parse(dob_text.getText()).getTime());

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...