Write code to convert String in various format To Date
//package com.book2s; import java.sql.Date; import java.text.SimpleDateFormat; public class Main { public static void main(String[] argv) { String sMt = "12:12:12"; System.out.println(convertStringToDate(sMt)); }/*from w w w . j ava 2 s. c o m*/ public synchronized static Date convertStringToDate(String sMt) { Date result = null; try { java.util.Date utilDate = null; if (sMt != null && sMt.length() > 0) { int length = sMt.length(); if (length > 17) { utilDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") .parse(sMt); } else if (length > 13) { utilDate = new SimpleDateFormat("yyyy-MM-dd HH:mm") .parse(sMt); } else if (length >= 10) { utilDate = new SimpleDateFormat("yyyy-MM-dd") .parse(sMt); } else if (length >= 8) { utilDate = new SimpleDateFormat("HH:mm:ss").parse(sMt); } else if (length == 7) { utilDate = new SimpleDateFormat("yyyy-MM").parse(sMt); } else if (length == 5) { utilDate = new SimpleDateFormat("HH:mm").parse(sMt); } result = new Date(utilDate.getTime()); } } catch (Exception e) { } return result; } }