-3

I have english date coming in string format such as 2011-12-12. I need to convert it into Date format so I tried :

 String assignDates="2011-12-12";
    DateFormat df = new SimpleDateFormat("YYYY-MM-dd");
    Date dates = df.parse(assignDates);
    cstmt.setDate(2,dates);

But i need to set it into cstmt.setDate(2,dates); but it is showing me error in this line like this:

The method setDate(int, java.sql.Date) in the type PreparedStatement is not applicable for the arguments (int, java.util.Date)

enter image description here

The full code is:

public String getConvertedDateToBs(String assignDates) {
        try
        {
            DateFormat df = new SimpleDateFormat("YYYY-MM-dd");
            System.out.println("yo ni chiryo"+assignDates);
        conn = DriverManager.getConnection(dbProperties.getDatabaseUrl());
        System.out.println("chiryoassignDatea");
            CallableStatement cstmt = conn.prepareCall("{? = call PCFN_LIB_ETON(?)}");
            cstmt.registerOutParameter(1,Types.VARCHAR);
            //java.sql.Types.VARBINARY
            Date dates = df.parse(assignDates);
            cstmt.setDate(2,dates);
            cstmt.executeUpdate();
            dateInBs = cstmt.getString(1);

            /*dateInBs = df.format(assignBsDate);*/
            System.out.println(dateInBs);
        }
        catch (SQLException e)
        {
          System.err.println(e.getMessage());
        }
        return dateInAd;
    }
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
ashwin karki
  • 643
  • 5
  • 19
  • 35
  • your error already explain. `setDate` method need `java.sql.Date` type. But, your date is `java.util.Date` type -> change `cstmt.setDate(2, new java.sql.Date(dates.getTime()))` – Zaw Than oo Jan 22 '19 at 05:06
  • Problem with imports? java.sql.Date or java.util.Date – Mohamed Anees A Jan 22 '19 at 05:07
  • First verify the format specifiers are what you really want - [`SimpleDateFormat`](https://docs.oracle.com/javase/10/docs/api/java/text/SimpleDateFormat.html); Second consider using the `java.time` API instead ([this example](https://stackoverflow.com/questions/18614836/using-setdate-in-preparedstatement) shows you how); Third, the error message seems pretty obvious, you are using a `java.util.Date` when the method is expecting a `java.sql.Date` - you have the wrong type – MadProgrammer Jan 22 '19 at 05:08
  • 1
    Not what you asked, but you want `yyyy-MM-dd`. `Y` is different from `y` - it was added to Java to catch out people who don't read Javadocs. – Dawood ibn Kareem Jan 22 '19 at 05:10
  • @MohamedAneesA i have imported import java.util.Date; – ashwin karki Jan 22 '19 at 05:10
  • 1
    Actually, the simplest thing to do is to import `java.sql.Date` but NOT `java.util.Date`. Then you can write `Date.valueOf(yourString)` and all your problems will go away. If you need `java.util.Date` for some other reason (and you _really_ don't, since it's kind of obsolete these days), you can just write `java.sql.Date.valueOf(yourString)` without importing `java.sql.Date`. – Dawood ibn Kareem Jan 22 '19 at 05:12

1 Answers1

3

Assuming you are using at least JDBC version 4.2 (which you probably are), it’s much easier and more straightforward than you think (not tested):

        LocalDate date = LocalDate.parse(assignDates);
        cstmt.setObject(2, date);

No need for DateFormat/SimpleDateFormat and no need for java.util.Date nor java.sql.Date. Which is very good because those classes had design problems, the first in particular are notoriously troublesome. All of these old classes are considered long outdated.

Instead use LocalDate from java.time, the modern Java date and time API. The modern API is much nicer to work with.

I am taking advantage of the fact that your string, 2011-12-12, is in ISO 8601 format, the format that the modern date and time classes parse (and also print) as their default, that is, without any explicit formatter.

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