Java SQL Date Parse strToSqlDate(String format, String value)

Here you can find the source of strToSqlDate(String format, String value)

Description

str To Sql Date

License

Apache License

Declaration

public static java.sql.Date strToSqlDate(String format, String value) throws ParseException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static java.sql.Date strToSqlDate(String format, String value) throws ParseException {
        Date utilDate = strToJavaDate(format, value);
        return convertJavaDateToSqlDate(utilDate);
    }/*from  www  .ja  va  2 s.  c  o  m*/

    public static Date strToJavaDate(String format, String value) throws ParseException {
        Date date = null;
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            formatter.setLenient(false); //make it strict
            date = formatter.parse(value);
        } catch (ParseException e) {
            throw new ParseException("Failed to parse the " + value + " to Date by format[" + format + "].", 1);
        }
        return date;
    }

    public static java.sql.Date convertJavaDateToSqlDate(Date javaDate) throws ParseException {
        java.sql.Date sqlDate = null;
        if (javaDate != null) {
            sqlDate = new java.sql.Date(javaDate.getTime());
        }
        return sqlDate;
    }
}

Related

  1. strToDate(String str)
  2. strToDate(String strDate, String formator)
  3. strToDate(String stringDate)
  4. StrToDate(String val)
  5. strToDate(String value, String pattern)
  6. strToUtilDate(String fecha, String hora)