Here you can find the source of getSqlDateFromStr(DateFormat format, String s, boolean need_default)
public static java.sql.Date getSqlDateFromStr(DateFormat format, String s, boolean need_default)
//package com.java2s; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class Main { public final static DateFormat format_yyyyMMdd_HHmmss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public final static DateFormat format_yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd"); public final static DateFormat format13 = new SimpleDateFormat("yyyy-MM-dd HH:mm"); public static java.sql.Date getSqlDateFromStr(DateFormat format, String s, boolean need_default) { Date utilDate = getDateFromStr(format, s); return utilDate_to_sqlDate(utilDate, need_default); }/* w ww . j a va 2 s. c o m*/ public static Date getDateFromStr(DateFormat format, String s) { if (s == null || s.trim().equals("")) { return null; } if (format == null) { if (s.length() == 10) { format = format_yyyyMMdd; } else if (s.length() == 16) { format = format13; } else { format = format_yyyyMMdd_HHmmss; } } return parseDateStringToDate(format, s); } public static java.sql.Date utilDate_to_sqlDate(Date utilDate, boolean need_default) { if (utilDate == null) { if (need_default) { return new java.sql.Date(0);// 1970-01-01 } return null; } return new java.sql.Date(utilDate.getTime()); } public static Date parseDateStringToDate(DateFormat format, String dateString) { if (format != null) { synchronized (format) { try { return format.parse(dateString); } catch (ParseException e) { return null; } } } else { // String message = "format is null,the dataString is " + // dateString; return null; } } public static Date parseDateStringToDate(DateFormat format, String time, TimeZone timeZone) { synchronized (format) { try { format.setTimeZone(timeZone); return format.parse(time); } catch (ParseException e) { return null; } } } }