convert string date to java.sql.Date

java, sql

Solution

This works for me without throwing an exception:

package com.sandbox;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Sandbox {

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Date parsed = format.parse("20110210");
        java.sql.Date sql = new java.sql.Date(parsed.getTime());
    }


}

Problem

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")

Original source