Java SQL Date Get getDate(String date)

Here you can find the source of getDate(String date)

Description

This method converts date provided as a String into JDBC format.

License

Open Source License

Parameter

Parameter Description
date date in "dd-MM-yyyy" format

Return

JDBC formatted instance of the day throws ParseException will be thrown if the date format of the is invalid

Declaration

public static java.sql.Date getDate(String date) throws Exception 

Method Source Code

//package com.java2s;
/*//w  ww.  j  a  va 2 s .  c o  m
 * @(#)ConversionUtil.java
 *
 * Copyright by ObjectFrontier, Inc.,
 * 12225 Broadleaf Lane, Alpharetta, GA 30005, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of ObjectFrontier, Inc. You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of
 * the license agreement you entered into with ObjectFrontier.
 */

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

public class Main {
    protected static SimpleDateFormat userDateFmt = new SimpleDateFormat("dd-MM-yyyy");
    protected static SimpleDateFormat rtgsUserDateFmt = new SimpleDateFormat("yyyyMMdd");

    /**
     * This method converts date provided as a String into JDBC format.
     *
     * @param   date date in "dd-MM-yyyy" format
     *
     * @return  JDBC formatted instance of the day
     *
     * throws   ParseException will be thrown if the date format of the is invalid
     */
    public static java.sql.Date getDate(String date) throws Exception {

        try {
            if ((date == null) || (date.trim().length() == 0))
                return null;

            return new java.sql.Date(userDateFmt.parse(date).getTime());
        } catch (ParseException e) {
            //RBC    
            try {
                if ((date == null) || (date.trim().length() == 0))
                    return null;
                return new java.sql.Date(rtgsUserDateFmt.parse(date).getTime());
            } catch (ParseException pe) {
                throw new Exception(
                        "Invalid Date Format(Expected:DD-MM-YYYY or yyyyMMdd). Check Date, Month & Year.");
            }
        }
    }
}

Related

  1. getDate(ResultSet rs, String column)
  2. getDate(ResultSet rs, String columnLabel)
  3. getDate(ResultSet rs, String columnName)
  4. getDate(String aDate, int dif)
  5. getDate(String date)
  6. getDate(String format)
  7. getDate(String pattern)
  8. getDate(String strDate)
  9. getDate(String theDateStr, int days)