Here you can find the source of strToDatetime(String sStr)
public static Date strToDatetime(String sStr)
//package com.java2s; //License from project: Apache License import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static Date strToDatetime(String sStr) { if (sStr == null) { return null; }/*from w w w .j a v a2s . c o m*/ SimpleDateFormat formatter; if (sStr.length() == 19) { formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } else if (sStr.length() == 10) { formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } else if (sStr.length() == 8) { formatter = new SimpleDateFormat("yyyyMMdd"); } else if (sStr.length() == 14) { formatter = new SimpleDateFormat("yyyyMMddHHmmss"); } else { formatter = new SimpleDateFormat("yyyyMMddHHmmss"); } ParsePosition pos = new ParsePosition(0); return formatter.parse(sStr, pos); } }