1

I am facing difficulties while trying to insert a date variable into a database table. My variable is called:

Date date4;

The date4 variable value is read from a textbox which has a calendar picker. For inserting in a date column, I am setting the field type to date:

preparedstmt.setDate(4,date4);

However, I got the below message after submitting the form:

javax.faces.component.UpdateModelException: java.lang.IllegalArgumentException: Cannot convert 4/1/15 12:00 AM of type class java.util.Date to class java.sql.Date

Also, is there a way to insert the field in the format of date ("dd-MON-YYYY")?

Learner30
  • 79
  • 1
  • 10
  • 1
    This question has been asked and answered many times before on this site and elsewhere, q.v. [this post](http://stackoverflow.com/questions/530012/how-to-convert-java-util-date-to-java-sql-date) for example. – Tim Biegeleisen Apr 01 '15 at 14:07
  • Can I insert the format of date ("dd-MON-YYYY") in a date column? – Learner30 Apr 01 '15 at 14:09

1 Answers1

1

Convert util date to sql date

    java.sql.Date sqlDate = new java.sql.Date(date4.getTime());
M Sach
  • 33,416
  • 76
  • 221
  • 314
  • Thanks for the answer... Is there a way is there a way to insert the field in the format of date ("dd-MON-YYYY") of type date? – Learner30 Apr 01 '15 at 14:06
  • From java side you just specify that its sql. Now its upto DB vendor, how to represent that date. You need to check it for your DB vendor – M Sach Apr 01 '15 at 14:11