1
// 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);

            }
Oomph Fortuity
  • 5,710
  • 10
  • 44
  • 89

6 Answers6

2

You need to use java.util.Date as SimpleDateFormat deals with java.util.Date and not java.sql.Date.

Swapnil
  • 8,201
  • 4
  • 38
  • 57
2

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
Deepanshu J bedi
  • 1,530
  • 1
  • 11
  • 23
1

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);
SparkOn
  • 8,806
  • 4
  • 29
  • 34
0

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... 
Guus
  • 2,986
  • 2
  • 21
  • 32
0

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);
    }
    // ...
}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
0

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

Mustafa sabir
  • 4,130
  • 1
  • 19
  • 28