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