Java SimpleDateFormat parse String like "2020-02-13" to Date

Description

Java SimpleDateFormat parse String like "2020-02-13" to Date

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

public class Main{
    public static void main(String[] argv){
        String time = "2020-02-13";
        try {// w w w .  ja  v  a  2 s .  c o m
          System.out.println(StringToDate(time));
        } catch (ParseException e) {
          e.printStackTrace();
        }
    }
    public static Date StringToDate(String time) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.parse(time);
    }
}



PreviousNext

Related