How to parse String to java.sql.date

Jason Amavisca picture Jason Amavisca · Oct 11, 2012 · Viewed 19.7k times · Source

I have a String

String s = "01 NOVEMBER 2012";

Then I want parse it to sqlDate. And insert it into the database.

Is it possible to parse that string to sqlDate?!?!

Yup, sql date format is "yyyy-mm-dd"

Answer

jmj picture jmj · Oct 11, 2012

Use SimpleDateFormat to parse String date to java.util.Date

java.util.Date utilDate = new SimpleDateFormat("dd MMM yyyy").parse("01 NOVEMBER 2012");

and then convert it to java.sql.Date using millis

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