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

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

Related

how can i convert int to date in java? [duplicate]

This question already has answers here:
Good way to convert integer YYYYMMDD into java.util.Date with local time zone
(4 answers)
How can I convert an Integer (e.g 19000101 ) to java.util.Date?
(5 answers)
Type mismatch: cannot convert from java.util.Date to java.sql.Date
(3 answers)
Closed 2 years ago.
I want to convert an int value to a date.
For example, 20200605 is 2020-06-05
This is my code, Why doesn't it work?
public static void main(String[] args) {
int value = 19000101;
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");
Date date = originalFormat.parse(Integer.toString(value));
SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd");
String formatedDate = newFormat.format(date);
}
I am getting an error on this line.
Date date = originalFormat.parse(Integer.toString(value));
The error message is
Type mismatch: cannot convert from java.util.Date to java.sql.Date
I don't know what it means.
The error you get:
Type mismatch: cannot convert from java.util.Date to java.sql.Date
tells that you have imported a wrong Date object. Please check at the top of the class. You will need to replace import java.sql.Date; with import java.util.Date;
I don't know much about the Date and SimpleDateFormat classes in java, but this should be fairly easy to do anyway without it.
String date_str = ""+value;
String formattedDate = date_str.substring(0,4)+"-"+date_str.substring(4,6)+"-"+date_str.substring(6,8);

Convert String to Time Java.sql.time error [duplicate]

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

Convert util.date to sql.date with date and time in java [duplicate]

This question already has answers here:
java.util.Date to java.sql.Date doesn't contain Time
(4 answers)
Closed 6 years ago.
I am trying to convert java.util.Date into java.sql.Date with below function but this function is giving the time a s 00:00:00.000.
private java.sql.Date getCurrentDate() {
java.util.Date today = new java.util.Date();
return new java.sql.Date(today.getTime());
}
please help me how can I get both date and time in java.sql.date.
thanks
Converting java.util.Date to java.sql.Date will lost the hour,minute and second.
So if it is possible, I suggest you use java.sql.Timestamp.
final String str = "INSERT INTO table (curTime) VALUES( ?)";
final PreparedStatement preparedStatement =connection.prepareStatement(str);
preparedStatement.setTimestamp(1, new Timestamp(date.getTime()));
you have to use a timestamp for that its not possible with java.sql.date since it removes data related to your time.

java.lang.ClassCastException: java.util.Date

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

SimpleDateFormat gives java.lang.classcastexception: java.util.date

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

Categories