Here you can find the source of stringToDate(String date, String pattren)
public static Date stringToDate(String date, String pattren)
//package com.java2s; /**// w ww.j a v a 2 s .c o m * Copyright ? 2013 Aftab Mahmood * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details <http://www.gnu.org/licenses/>. **/ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static Date stringToDate(String date, String pattren) { if (date == null || date.trim().isEmpty()) return null; if (pattren != null && !pattren.trim().isEmpty()) { DateFormat formater = new SimpleDateFormat(pattren); try { return formater.parse(date); } catch (Exception e) { e.printStackTrace(); } } return null; } public static Date stringToDate(String date, String time, String pattren) throws ParseException { if (time == null || time.trim().isEmpty()) time = "00:00:00,000"; return stringToDate(date + " " + time, pattren); } }