// I am trying to send date to my dao class but i am getting exception
String dateInString = request.getParameter("date");
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
// i am importing java.sql.Date
//i am getting java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
date = (Date) sdf.parse(dateInString);
} catch (ParseException e) {
e.printStackTrace();
}
LeaveBalance leave = new LeaveBalance(associateID, date,
bufferApplicable, buffer);
assoicatesDAO = new AssociatesDAO();
boolean success = assoicatesDAO.leaveBalance(leave);
if (success) {
msg = "Associate's leave has been added successfully.";
} else {
msg = "****";
}
request.setAttribute("responseMessage", msg);
dispatcher = request.getRequestDispatcher("/jsp/success.jsp");
dispatcher.forward(request, response);
}

- 5,710
- 10
- 44
- 89

- 9
- 2
-
3Import java.util.Date instead? – Chris Byatt Jul 25 '14 at 09:54
-
Check what format date your are receiving form "date" parameter – Prashant Gurav Jul 25 '14 at 09:56
-
or explicitly cast to java.util.Date -> date = (java.util.Date) sdf.parse(dateInString); – Danny. Jul 25 '14 at 09:56
-
`java.sql.Date sqlDate = new java.sql.Date(sdf.parse(dateInString).getTime()); ` – Subhrajyoti Majumder Jul 25 '14 at 09:56
-
Possible duplicate of http://stackoverflow.com/questions/530012/how-to-convert-java-util-date-to-java-sql-date?lq=1? – DavidPostill Jul 25 '14 at 10:02
6 Answers
You need to use java.util.Date
as SimpleDateFormat
deals with java.util.Date
and not java.sql.Date
.

- 8,201
- 4
- 38
- 57
cast exception is Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.
So, for example, when one tries to cast an Integer to a String, String is not an subclass of Integer, so a ClassCastException will be thrown.
Object i = Integer.valueOf(42);
String s = (String)i; // ClassCastException thrown here.
here in your code you should
import java.util.Date;
date = sdf.parse(dateInString); //thanks to @ Mustafa sabir

- 1,530
- 1
- 11
- 23
-
1Explicit cast of `(Date)` is not required, `parse()` returns type of `java.util.Date` – Mustafa sabir Jul 25 '14 at 10:00
-
@Mustafasabir but the code now is working for me? still if You say i will try that. – Deepanshu J bedi Jul 25 '14 at 10:01
-
@DeepanshuBedi The topic of this question implies a learning programmer, the explanation is good to have. In the event OP understands the exception, he can reference the other answers anyway. :) – Unihedron Jul 25 '14 at 10:01
-
1Yes it will work, but even if you remove it will work , its not wrong, just that you are giving cast of the same type to the same object, which is redundant :) – Mustafa sabir Jul 25 '14 at 10:03
-
use import java.util.Date
instead of java.sql.Date
SimpleDateFormatObject.parse()
will return Date of type java.util.Date
so in that case you will never need to cast it as you are doing in here
date = (Date) sdf.parse(dateInString);

- 8,806
- 4
- 29
- 34
java.sql.Date is a subclass of java.util.Date. This means that sql.Date contains more details than it's superclass (it is more specialized). This is why you cannot cast a util.Date to a sql.Date: there is no way for the JVM to 'fill in the blanks'. The other way around is not a problem.
For a solution, you could import java.util.Date instead of java.sql.Date, or, if that breaks other part of the code, simply use the fully qualified class name in your code:
java.util.Date date = null;
// etc...

- 2,986
- 2
- 21
- 32
Your import
declaration of this class is java.sql.Date
. As SimpleDateFormat.parse(String)
returns java.util.Date
, you may have to import the proper Date
from java.util
instead.
However in the event that java.sql.Date
is required for the rest of your code and you must keep the import declaration as same, change the following:
{
// Declare date as proper Date
java.util.Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
/* // Cast to proper Date
* date = (java.util.Date) sdf.parse(dateInString);
* This cast is actually redundant, so just use:
*/
date = sdf.parse(dateInString);
}
// ...
}

- 10,902
- 13
- 62
- 72
The problem is your Date date
variable is of type java.sql.Date
, while the sdf.parse()
returns java.util.Date
.Hence the exception at date=sdf.parse(..)
. Import java.util.Date
instead of java.sql.Date

- 4,130
- 1
- 19
- 28