Java SQL Date From toSqlDate(final String _text, final String _dateFormat)

Here you can find the source of toSqlDate(final String _text, final String _dateFormat)

Description

Convert a string into a java.sql.Date.

License

Open Source License

Parameter

Parameter Description
_text The string to parse.

Return

The java.sql.Date object.

Declaration

public static java.sql.Date toSqlDate(final String _text,
        final String _dateFormat) 

Method Source Code

//package com.java2s;
/*//from   ww w  . j  ava2s.  co  m
 * $Id$
 * ============================================================================
 * Project grooocle
 * Copyright (c) 2008-2010 by Andre Winkler. All rights reserved.
 * ============================================================================
 *          GNU LESSER GENERAL PUBLIC LICENSE
 *  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
 *
 *  This library 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 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * Convert a date type into a <code>java.sql.Date</code>.
     * 
     * @param _date
     *            The date to convert.
     * 
     * @return The java.sql.Date object.
     */
    public static java.sql.Date toSqlDate(final Date _date) {
        return new java.sql.Date(_date.getTime());
    }

    /**
     * Convert a string into a <code>java.sql.Date</code>.
     * 
     * @param _text
     *            The string to parse.
     * 
     * @return The java.sql.Date object.
     */
    public static java.sql.Date toSqlDate(final String _text) {
        return toSqlDate(toDate(_text));
    }

    /**
     * Convert a string into a <code>java.sql.Date</code>.
     * 
     * @param _text
     *            The string to parse.
     * 
     * @return The java.sql.Date object.
     */
    public static java.sql.Date toSqlDate(final String _text,
            final String _dateFormat) {
        return toSqlDate(toDate(_text, _dateFormat));
    }

    /**
     * Convert a string into a date type. Throws a runtime exception if string
     * is unparseable.
     * 
     * @param _text
     *            The string to parse.
     * 
     * @return The date object.
     */
    public static Date toDate(final String _text) {
        return (toDate(_text, DATE_FORMAT));
    }

    /**
     * Convert a string into a date type. Throws a runtime exception if string
     * is not to parse.
     * 
     * @param _text
     *            The string to parse.
     * 
     * @param _dateForamt
     *            A format like <code>yyyy-MM-dd HH24:MI:ss</code>
     * 
     * @return The date object.
     */
    public static Date toDate(final String _text, final String _dateFormat) {
        SimpleDateFormat formatter = new SimpleDateFormat(_dateFormat);
        try {
            return formatter.parse(_text);
        } catch (ParseException ex) {
            throw new RuntimeException(ex);
        }
    }
}

Related

  1. toSqlDate(Date date)
  2. toSqlDate(Date date)
  3. toSqlDate(Date date)
  4. toSqlDate(Date dt)
  5. toSqlDate(final Date date)
  6. toSQLDate(java.util.Date date)
  7. toSqlDate(java.util.Date date)
  8. toSQLDate(java.util.Date date)
  9. toSQLDate(Object object, Object oDefault)