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
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.
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
Related
This question already has answers here:
How to convert java.util.Date to java.sql.Date?
(17 answers)
Closed 5 years ago.
I want to convert a string to time using the parse method which is going to be inserted to database later. But I get: Incompatible Types: Java.util.date cannot be converted to Java.sql.Date. Any Solution?
String s = time.getText();
DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date d = sdf.parse(s);
SimpleDateFormat parse() method returns java.util.Date whereas you have imported java.sql.Date, so change the imported class to java.util.Date
the errors occurs when java.sql.Date imported which is derived from java.util.Date, and DateFormat.parse return a java.util.Date. so you can't assign java.util.Date to derived type java.sql.Date.
import java.util.Date;
//not
import java.sql.Date;
OR assign it to a full-name type of java.util.Date when you both using java.sql.Date and java.util.Date:
java.util.Date d = sdf.parse(s);
OR if you need convert java.util.Date to a java.sql.Date you can do as follows:
java.sql.Date d = new java.sql.Date(sdf.parse(s).getTime());
OR if you need convert java.util.Date to a java.sql.Time you can do as follows:
java.sql.Time d = new java.sql.Time(sdf.parse(s).getTime());
This question already has answers here:
How to convert java.util.Date to java.sql.Date?
(17 answers)
Closed 7 years ago.
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
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 to java.sql.Date, you need to use the java.sql.Date constructor and java.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());
Change your format to yyyy-MM-dd.
I just wrote this program and it works fine. Make sure you aren't getting some other error now.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class DateFormatDemo
{
public static void main(String[] args) throws ParseException
{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
System.out.println(df.parse("1994-09-09"));
}
}
Your first try probably threw the exception you mentioned because of the wrong format as Josh pointed out. After correcting this the next problem occurs:
A java.sql.Date is NOT a java.util.Date. So you cannot just typecast the outcome of the df.parse, which is a java.util.Date.
And third: If you provide the pattern to the SimpleDateFormat you can omit the locale.
Following code runs without errors:
String input = "1994-09-09";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(input);
System.out.println( date);
java.sql.Date sqlDate = new java.sql.Date( date.getTime() );
System.out.println( sqlDate);
I have a problem when getting a Date object from a ResultSet. In database it has a value (for example 2014-08-01) and after getting it from resultSet, it has another value (2014-08-31). I know that ResultSet's getDate method returns java.sql.Date, but I tried a few solutions, such as:
Date date=new java.util.Date(resultSet.getDate(3).getTime());
or
Date date=resultSet.getTimestamp();
but the problem was the same.
If I try
Date date=resultSet.getDate();
It throws a NullPointerException.
Can anybody explain this?
In your case you were not providing the columnName of the Date field to be retrieved.
This should do the job
while (rs.next()) {
java.sql.Time dbSqlTime = rs.getTime("columnName");
java.sql.Date dbSqlDate = rs.getDate("columnName");
java.sql.Timestamp dbSqlTimestamp = rs.getTimestamp("columnName");
java.util.Date dbSqlTimeConverted = new java.util.Date(dbSqlTime.getTime());
java.util.Date dbSqlDateConverted = new java.util.Date(dbSqlDate.getTime());
System.out.println(dbSqlTimeConverted);
System.out.println(dbSqlDateConverted);
}
iterate over the ResultSetObject get the Date from the ResultSetObject which is java.sql.Date then convert it to java.util.Date
You should use the java.sql.Date instead of java.util.Date, because ResultSet.getDate() returns an SQL Date, and not a Java one.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert java.util.date to java.sql.date?
I found error on my function, it shows error result after initializing the newInstance() method from DatatypeFactory df , I'm getting another error:
java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
I just change the package name from
java.util.Date into java.SQL.Date
then casting:
Date dateStarting = (Date) jDateChooserStart.getDate();
Date dateEnding = (Date) jDateChooserEnd.getDate();
How to resolve this issue?
(post before: Convert jcalendar Date into XMLGregorianCalendar Getting Null Value)
It's not possible to cast from java.util.Date to java.sql.Date. You need to convert from one type to the other instead:
java.util.Date utilStartDate = jDateChooserStart.getDate();
java.sql.Date sqlStartDate = new java.sql.Date(utilStartDate.getTime());
From the class cast exception you can see that these are 2 distinct types and can't be cast the from one to the other. To convert from java.util.Date to java.sql.Date, you can use:
java.util.Date date = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
String str = "13/06/2011";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = (Date)formatter.parse(str);
I guess that your Date class is actually a java.sql.Date.
What does your import statement say? Are you importing some other class (for example java.sql.Date) by accident? What does the compiler say when you remove the class cast (which should not be there)?
DateFormat.parse() returns an instance of java.util.Date and not java.sql.Date.
In order to convert from java.util.Date to java.sql.Date, I do the following:
java.util.Date fromDate = df.parse(fromdate1);
java.sql.Date sqlDate = new java.sql.Date(fromDate.getTime());