Java - Write code to parse String To Date in 2019/01/01 format

Requirements

Write code to parse String To Date in 2019/01/01 format

Demo

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

public class Main {
    public static void main(String[] argv) {
        String str = "2019/01/01";
        System.out.println(StringToDate(str));
    }/*from  ww w  .  j  a v  a 2 s.c o  m*/

    public static Date StringToDate(String str) {
        Date date = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
        try {
            date = formatter.parse(str);
        } catch (ParseException e) {
            throw new IllegalArgumentException(
                    "Wrong date format. Use yyyy/mm/dd.");
        }
        return date;
    }
}

Related Exercise