I have imported java.sql.*;
but it shows no suitable constructor found for
Date() Date
d=new Date();
what does it mean? I got confused: is Date
class is available in sql package or not? I also import java.util.Date
.
I have imported java.sql.*;
but it shows no suitable constructor found for
Date() Date
d=new Date();
what does it mean? I got confused: is Date
class is available in sql package or not? I also import java.util.Date
.
Let me start by stepping a bit aside: the real and good solution to your problem is to avoid the long outdated java.sql.Date
class completely. It used to be used for storing dates into SQL databases and retrieve them back, but today you are much better off using LocalDate
from java.time, the modern Java date and time API, for that. To obtain the current date:
LocalDate ld = LocalDate.now(ZoneId.of("Asia/Shanghai"));
Please substitute the desired time zone if it didn’t happen to be Asia/Shanghai.
However, to answer your question: When you type new Date()
you are trying to invoke a constructor without any arguments. This requires a constructor without any parameters. Many classes have such a constructor, but not all classes.
Now look at the API documentation of the java.sql.Date
class. You will see that it has got two constructors, Date(int year, int month, int day)
and Date(long date)
(the former is deprecated, meaning avoid it if there’s any way you can). Both constructors have parameters, so none of them matches your attempt to invoke one without arguments. In other words, none of them is suitable for the invocation you tried. This is what the message means.
The class is certainly available in the java.sql
package. The problem is only with the constructor (and with the class being outmoded, but I am repeating myself).