Java String to Date toDate(final String _text, final String _dateFormat)

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

Description

Convert a string into a date type.

License

Open Source License

Parameter

Parameter Description
_text The string to parse.
_dateForamt A format like <code>yyyy-MM-dd HH24:MI:ss</code>

Return

The date object.

Declaration

public static Date toDate(final String _text, final String _dateFormat) 

Method Source Code

//package com.java2s;
/*/*from   w  ww  .j  a  v  a  2s  .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 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. strToDateWithFormat(String strDate, Format format)
  2. TimeStringToDate(String s)
  3. timeToDate(String time)
  4. toDate(DateFormat format, String value)
  5. toDate(final Long date, final String pattern)
  6. toDate(final String date, final String time)
  7. toDate(final String dateString, final String format)
  8. toDate(final String dateString, final String pattern)
  9. toDate(int date, String timeFormat)