[java] convert string date to java.sql.Date

Is it possible to convert string "20110210" to a java.sql.Date 2011-02-10?

I've tried SimpleDateFormat and I get java.text.ParseException: Unparseable date: "20110210"

What am I doing wrong?

i had new SimpleDateFormat("yyyy-MM-dd") instead of new SimpleDateFormat("yyyyMMdd")

This question is related to java sql

The answer is


worked for me too:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Date parsed = null;
    try {
        parsed = sdf.parse("02/01/2014");
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    java.sql.Date data = new java.sql.Date(parsed.getTime());
    contato.setDataNascimento( data);

    // Contato DataNascimento era Calendar
    //contato.setDataNascimento(Calendar.getInstance());         

    // grave nessa conexão!!! 
    ContatoDao dao = new ContatoDao("mysql");           

    // método elegante 
    dao.adiciona(contato); 
    System.out.println("Banco: ["+dao.getNome()+"] Gravado! Data: "+contato.getDataNascimento());