I am trying to use a java.util.Date
as input and then creating a query with it - so I need a java.sql.Date
.
I was surprised to find that it couldn't do the conversion implicitly or explicitly - but I don't even know how I would do this, as the Java API is still fairly new to me.
try with this
I think the best way to convert is:
If you want to insert the
dt
variable into an SQL table you can do:i am using the following code please try it out
specify the format of the date you want for example
"DD-MM_YYYY"
or'YYYY-mm-dd'
then use the java Date datatype asthen it will parse your date
I was trying the following coding that worked fine.
If you are usgin Mysql a date column can be passed a String representation of this date
so i using the DateFormatter Class to format it and then set it as a String in the sql statement or prepared statement
here is the code illustration:
String date = converUtilDateToSqlDate(otherTransaction.getTransDate());
//then pass this date in you sql statement
With the other answer you may have troubles with the time info (compare the dates with unexpected results!)
I suggest: