Java SQL Date Parse parseDateString(String tmp, String format)

Here you can find the source of parseDateString(String tmp, String format)

Description

Takes a string and tries to convert it to a Date based on the specified formatting

License

Open Source License

Parameter

Parameter Description
tmp Description of the Parameter
format Description of the Parameter

Return

Description of the Return Value

Declaration

public static java.sql.Date parseDateString(String tmp, String format) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;

public class Main {
    /**//  w  w w .jav a  2  s. c  o m
     *  Takes a string and tries to convert it to a Date
     *
     * @param  tmp  Description of the Parameter
     * @return      Description of the Return Value
     */
    public static java.sql.Date parseDateString(String tmp) {
        java.sql.Date dateValue = null;
        try {
            java.util.Date tmpDate = DateFormat.getDateInstance(DateFormat.SHORT).parse(tmp);
            dateValue = new java.sql.Date(new java.util.Date().getTime());
            dateValue.setTime(tmpDate.getTime());
            return dateValue;
        } catch (Exception e) {
            try {
                return java.sql.Date.valueOf(tmp);
            } catch (Exception e2) {
            }
        }
        return null;
    }

    /**
     *  Takes a string and tries to convert it to a Date based on the specified
     *  formatting
     *
     * @param  tmp     Description of the Parameter
     * @param  format  Description of the Parameter
     * @return         Description of the Return Value
     */
    public static java.sql.Date parseDateString(String tmp, String format) {
        java.sql.Date dateValue = null;
        SimpleDateFormat df = null;
        df = new SimpleDateFormat(format);
        try {
            java.util.Date tmpDate = df.parse(tmp, new ParsePosition(0));
            dateValue = new java.sql.Date(new java.util.Date().getTime());
            dateValue.setTime(tmpDate.getTime());
            return dateValue;
        } catch (Exception e) {
            try {
                return java.sql.Date.valueOf(tmp);
            } catch (Exception e2) {
            }
        }
        return null;
    }
}

Related

  1. parseDate(String strDate)
  2. parseDate(String strFormat, String dateValue)
  3. parseDate(String value, String format)
  4. parseDate2(Date date, String pattern)
  5. parseDateE(Date dt)
  6. parseDateYYMMddHHmm(java.util.Date date)
  7. parseDateYYMMddHHmmss1(Date date)
  8. parseDateyyyy_MM_DD(String ds)
  9. parseSqlDate(final String value)