Here you can find the source of stringToDate(String dateString, String formatStr)
public static Date stringToDate(String dateString, String formatStr) throws ParseException
//package com.java2s; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static Date stringToDate(String dateString, String formatStr) throws ParseException { if (isNotEmpty(dateString) && isEmpty(formatStr)) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return dateFormat.parse(dateString); } else if (isNotEmpty(dateString) && isNotEmpty(formatStr)) { SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr); return dateFormat.parse(dateString); }//from ww w. j av a 2s . c om return null; } public static Date stringToDate(String dateString) throws ParseException { if (isNotEmpty(dateString)) { return stringToDate(dateString, null); } return null; } public static boolean isNotEmpty(String s) { if (null != s && !s.equals("")) { return true; } return false; } public static boolean isEmpty(String s) { if (null == s || s.equals("")) { return true; } return false; } }