Get the current date in java.sql.Date format

java, java.util.date, jdbc

Solution

A `java.util.Date` is not a `java.sql.Date`. It's the other way around. A `java.sql.Date` is a `java.util.Date`.

You'll need to convert it to a `java.sql.Date` by using the constructor that takes a `long` that a `java.util.Date` can supply.

java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

Problem

I need to add the current date into a prepared statement of a JDBC call. I need to add the date in a format like `yyyy/MM/dd`. I've try with ``` DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); Date date = new Date(); pstm.setDate(6, (java.sql.Date) date); ``` but I have this error: ``` threw exception java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date ``` Is there a way to obtain a `java.sql.Date` object with the same format?

Original source

Related problems