Java - Write code to parse string like 2018-10-12 01:01:02 to Date Time

Requirements

Write code to parse string like 2018-10-12 01:01:02 to Date Time

Demo

//package com.book2s;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;

public class Main {
    public static void main(String[] argv) {
        String date = "2018-10-12 01:01:02";
        System.out.println(stringConvertDateTime(date));
    }//from w  ww .j  a v  a2 s. com

    public static Date stringConvertDateTime(String date) {
        // TODO Auto-generated method stub
        SimpleDateFormat format = new SimpleDateFormat(
                "yyyy-MM-dd HH-mm-ss");
        Date d = null;
        try {
            d = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }
}

Related Exercise