0

i am trying to insert Date into database column type Date getting java.lang.ClassCastException: java.util.Date

code:

Date dateFormatter = (Date) new SimpleDateFormat("dd/MM/yyyy").parse(requiredByDate.toString());
java.sql.Date requiredByDate1 = new java.sql.Date(dateFormatter.getTime());

set to prepared statement:

pstmt.setDate(1, requiredByDate1);

got a error

 java.lang.ClassCastException: java.util.Date

pls suggest how to resolve this.

Thanks

2
  • post your code and the exact stack trace Commented Oct 16, 2014 at 9:58
  • 1
    you shouldn't even need to cast it in the first place Commented Oct 16, 2014 at 10:00

2 Answers 2

1

Your code and the error message does not match. You have to be using some other version of the code.

You have:

java.sql.Date requiredByDate1 = ...
pstmt.setDate(1, requiredByDate1);

but that code cannot result in

java.lang.ClassCastException: java.util.Date

as it's not java.util.Date.

Sign up to request clarification or add additional context in comments.

Comments

0

Your Code Here is :

Date dateFormatter = (Date) new SimpleDateFormat("dd/MM/yyyy").
                                               parse(requiredByDate.toString());

Now Your Date may be either :

1) java.util.Date , OR

2) java.sql.Date , which ultimately is a Sub-class of java.util.Date.

However,

SimpleDateFormat class belongs to java.text package. And you are trying to cast 
an object of java.text.SimpleDateFormat to java.util.Date( OR java.sql.Date)
which is why you are getting java.lang.ClassCastException

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.