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
tojava.sql.Date
, you need to use thejava.sql.Date
constructor andjava.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