Calendar cal; String sql = "INSERT INTO ttable (dt) values (?);" //dt is a dateTime field in ttable PreparedStatement stmt = connection.prepareStatement(sql); stmt = setDate(1,cal); //not working stmt.execute(); stmt.close();
I would like to convert cal to a Date type to insert into table.
In order to get "the current date" (as in today's date), you can use LocalDate. now() and pass that into the java. sql. Date method valueOf(LocalDate) .
There is a getTime()
method (unsure why it's not called getDate).
Edit: Just realized you need a java.sql.Date. One of the answers which use cal.getTimeInMillis()
is what you need.
Use stmt.setDate(1, new java.sql.Date(cal.getTimeInMillis()))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With