Tuesday, July 10, 2018

datetime - error casting Java.Util.Date into 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)


Answer



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());

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...