Java String to Date toDate(String dateString)

Here you can find the source of toDate(String dateString)

Description

Attempts to convert a String representation of a date to a java.util.Date object.

License

Open Source License

Parameter

Parameter Description
dateString a parameter

Exception

Parameter Description
IllegalArgumentException if the date string cannot be parsed.

Return

a java.util.Date object

Declaration

public static Date toDate(String dateString) throws IllegalArgumentException 

Method Source Code


//package com.java2s;
/*//from   w  w  w.  j a v a2  s  .c  o  m
 * (C) 2007 - James G. Lombardo dba The Byteshop.Net
 * 
 * This code 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 code 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 program; if not, write to the Free 
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 
 * MA  02111-1307, USA.
 */

import java.util.Date;

import java.text.DateFormat;

public class Main {
    /**
     * Attempts to convert a String representation of a date to a java.util.Date 
     * object. Conversion rules are based on parse definitions in the 
     * java.text.DateFormat class.
     * @param dateString
     * @return a java.util.Date object
     * @throws IllegalArgumentException if the date string cannot be parsed.
     */
    public static Date toDate(String dateString) throws IllegalArgumentException {
        Date date = null;
        DateFormat df = DateFormat.getDateInstance();
        try {
            df = DateFormat.getDateInstance(DateFormat.SHORT);
            date = df.parse(dateString);
        } catch (Exception e) {
            try {
                df = DateFormat.getDateInstance(DateFormat.MEDIUM);
                date = df.parse(dateString);
            } catch (Exception e2) {
                try {
                    df = DateFormat.getDateInstance(DateFormat.LONG);
                    date = df.parse(dateString);
                } catch (Exception e3) {
                    try {
                        df = DateFormat.getDateInstance(DateFormat.FULL);
                        date = df.parse(dateString);
                    } catch (Exception e4) {
                        throw new IllegalArgumentException(e4.getMessage());
                    }
                }
            }
        }
        return date;
    }
}

Related

  1. toDate(String dateStr)
  2. toDate(String dateStr)
  3. toDate(String dateStr, String format)
  4. toDate(String dateStr, String pattern)
  5. toDate(String dateString)
  6. toDate(String dateString)
  7. toDate(String dateString, Locale locale)
  8. toDate(String dateString, String dateFormatPattern)
  9. toDate(String dateString, String format)