Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error casting Java.Util.Date into Java.Sql.Date [duplicate]

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)

like image 470
randytan Avatar asked Aug 26 '12 14:08

randytan


People also ask

How to convert sql Date into java Date?

If we have the Date object of the SQL package, then we can easily convert it into an util Date object. We need to pass the getTime() method while creating the util Date object. java.

What is the difference between Java Util date and Java SQL date?

sql. Date just represent DATE without time information while java. util. Date represents both Date and Time information.


1 Answers

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());
like image 82
João Silva Avatar answered Sep 23 '22 13:09

João Silva