Java Data Type How to - Format and parse Date '2013-11-15 00:00:00.0'








Question

We would like to know how to format and parse Date '2013-11-15 00:00:00.0'.

Answer

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/*  w  ww  . j a  va2  s.  co  m*/
public class Main{
    public static void main(String [] args) throws ParseException {
        String oldstring = "2013-11-15 00:00:00.0";

        java.util.Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(oldstring);

        String newstring = new SimpleDateFormat("yyyy-MM-dd").format(date);

        System.out.println(newstring);

    }

}