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());
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:
java.util.Date vs java.sql.Date
(7 answers)
Closed 5 years ago.
I am trying to insert Date into Database but I am getting error as java.util.Date cannot be cast to java.sql.Date.
Please help.
String next_dt = req.getParameter("NextDate");
DateFormat dtFmt = null;
dtFmt = new SimpleDateFormat("yyyy-MM-dd");
dtToday = (Date) dtFmt.parse(next_dt);
You have imported java.sql.Data. But dtFmt.parse(next_dt); returns an object of type java.util.Date so you have to change
import java.sql.Date;
to
import java.util.Date;
DateFormat.parse() returns a java.util.Date, and you're trying to illegally cast it to a java.sql.Date.
Assuming you continue to import java.sql.Date, you can successfully assign the variable like so:
dtToday = new Date(dtFmt.parse(next_dt).getTime());
You should use java.sql.Timestamp or java.sql.Date instead of java.util.Date
Problem with java.sql.Date is that it will not store time. So using Timestamp is the approach i always take. As it is the child class of java.util.date it is compatible with both date and timestamp columns in DB.
Add following lines - as it needs to be a sql Date and not util date
java.sql.Date sqlDate = new java.sql.Date(dtToday.getTime());
//now insert this sqlDate
public static java.sql.Date convertFromJAVADateToSQLDate(
java.util.Date javaDate) {
java.sql.Date sqlDate = null;
if (javaDate != null) {
sqlDate = new Date(javaDate.getTime());
}
return sqlDate;
}
As the name of the class is same use should give fully qualified name(FQN) of both classes you also can use format method to convert date to proper SQL format date.
public static String toMysqlDateStr(Date date) {
String dateForMySql = "";
if (date == null) {
dateForMySql = null;
} else {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateForMySql = sdf.format(date);
}
return dateForMySql;
}
I'm trying to create a variable called startDate using java.sql.Date.
I've tried...
java.sql.date startDate = "02/04/2015"
But it thinks it's a string.
java.sql.date startDate = 02/04/2015
But it thinks it's an int.
java.sql.date startDate = 02-04-2015
But it displays the error "invalid character constant".
How do I properly write this variable?
Thanks.
One possible approach is to use a SimpleDateFormat and the java.sql.Date(long) constructor like
DateFormat df = new SimpleDateFormat("MM-dd-yyyy");
java.sql.Date sqlDate = new java.sql.Date(df.parse("02-04-2015").getTime());
you could also try
java.sql.Date sqlDate = new java.sql.Date(2020,12,12);
I do not know why this is deprecated, but it is very intuitive for me.
basically year, month, day integer
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
If I have a method like this:
public static String convertDateTimeToString(DateTime dt) {
return dt.getDate() + " " + dt.getTime();
}
Which takes a DateTime object of my own which contains a java.sql.Date and a java.sql.Time, what is the best way of reversing the process so that I can substring a java.sql.Date and a java.sql.Time from a String?
Or if DateTime dt is a JodaTime DateTime object?
If this can be done without reference to java.util.Date.
Look at SimpleDateFormat class. Here's a tutorial.
You'll need something like this:
String datetimeString;
Date result;
SimpleDateFormat formatter = new SimpleDateFormat("EEE d MMM yy hh:mm:ss");
result = formatter.parse (datetimeString);
You can use the valueOf method of java.sql.Time to convert a String into a java.sql.Time, and use the valueOf method of java.sql.Date to convert a String into a java.sql.Date