Here you can find the source of parse(String dateString)
public static Date parse(String dateString)
//package com.java2s; //License from project: Open Source License import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.regex.Pattern; public class Main { public static Date parse(String dateString) { try {//from w w w . jav a 2 s . c om if (dateString == null || "".equals(dateString)) { return null; } SimpleDateFormat format = new SimpleDateFormat(); format.setLenient(false); if (Pattern.matches("\\d{4}\\-\\d{1,2}\\-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}.\\d{1,3}", dateString)) { format.applyPattern("yyyy-MM-dd HH:mm:ss.SSS"); } else if (Pattern.matches("\\d{4}\\-\\d{1,2}\\-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}", dateString)) { format.applyPattern("yyyy-MM-dd HH:mm:ss"); } else if (Pattern.matches("\\d{4}\\-\\d{1,2}\\-\\d{1,2} \\d{1,2}:\\d{1,2}", dateString)) { format.applyPattern("yyyy-MM-dd HH:mm"); } else if (Pattern.matches("\\d{4}\\-\\d{1,2}\\-\\d{1,2} \\d{1,2}", dateString)) { format.applyPattern("yyyy-MM-dd HH"); } else if (Pattern.matches("\\d{4}\\-\\d{1,2}\\-\\d{1,2}", dateString)) { format.applyPattern("yyyy-MM-dd"); } else if (Pattern.matches("\\d{4}\\-\\d{1,2}", dateString)) { format.applyPattern("yyyy-MM"); } else if (Pattern.matches("\\d{4}\\/\\d{1,2}\\/\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}.\\d{1,3}", dateString)) { format.applyPattern("yyyy/MM/dd HH:mm:ss.SSS"); } else if (Pattern.matches("\\d{4}\\/\\d{1,2}\\/\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}", dateString)) { format.applyPattern("yyyy/MM/dd HH:mm:ss"); } else if (Pattern.matches("\\d{4}\\/\\d{1,2}\\/\\d{1,2} \\d{1,2}:\\d{1,2}", dateString)) { format.applyPattern("yyyy/MM/dd HH:mm"); } else if (Pattern.matches("\\d{4}\\/\\d{1,2}\\/\\d{1,2} \\d{1,2}", dateString)) { format.applyPattern("yyyy/MM/dd HH"); } else if (Pattern.matches("\\d{4}\\/\\d{1,2}\\/\\d{1,2}", dateString)) { format.applyPattern("yyyy/MM/dd"); } else if (Pattern.matches("\\d{4}\\/\\d{1,2}", dateString)) { format.applyPattern("yyyy/MM"); } else if (Pattern.matches("\\d{4}\\d{1,2}\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}", dateString)) { format.applyPattern("yyyyMMdd HH:mm:ss"); } else if (Pattern.matches("\\d{4}\\d{1,2}\\d{1,2} \\d{1,2}:\\d{1,2}", dateString)) { format.applyPattern("yyyyMMdd HH:mm"); } else if (Pattern.matches("\\d{4}\\d{1,2}\\d{1,2} \\d{1,2}", dateString)) { format.applyPattern("yyyyMMdd HH"); } else if (Pattern.matches("\\d{4}\\d{1,2}\\d{1,2}", dateString)) { format.applyPattern("yyyyMMdd"); } else if (Pattern.matches("\\d{4}\\d{1,2}", dateString)) { format.applyPattern("yyyyMM"); } return format.parse(dateString); } catch (ParseException e) { e.printStackTrace(); } return null; } }