-8

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I commented on java.util then it show this error????? – Dheeraj Pratap Singh Jan 17 '18 at 14:50
  • 1
    What error? Show us some code so that we might be able understand your weird question... – Georg Leber Jan 17 '18 at 14:52
  • 1
    Learn to use the JavaDoc. See the constructor summary of [java.util.Date](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html) and [java.sql.Date](https://docs.oracle.com/javase/8/docs/api/java/sql/Date.html). – Thomas Fritsch Jan 17 '18 at 15:03
  • Search Stack Overflow thoroughly before posting. This topic has been addressed many times already. Specifically, search for “java.time” and “Instant”. – Basil Bourque Jan 17 '18 at 17:06
  • Possible duplicate of [JsonMappingException: No suitable constructor found for type \[simple type, class \]: can not instantiate from JSON object](https://stackoverflow.com/questions/7625783/jsonmappingexception-no-suitable-constructor-found-for-type-simple-type-class/7626872) – Ole V.V. Jan 17 '18 at 17:13
  • //import java.util.Date; import java.sql.*; class Test17 { public static void main(String[] args) { Date d=new Date(); System.out.println(d.getClass().getName()); } } – Dheeraj Pratap Singh Jan 17 '18 at 18:48
  • I had saw this topic on stack overflow but didn't get clarity that's why I posted – Dheeraj Pratap Singh Jan 17 '18 at 18:53
  • A tip: when you get an error message, paste it into your search engine. For *no suitable constructor found* you get lots of hits. You may add *Java* to the search to narrow down. – Ole V.V. Jan 17 '18 at 19:43

1 Answers1

1

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).

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161