Java Parse Date Pattern YYYY parse(String date)

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

Description

Analyzes the date string and tries to generate a java.util.Date.

License

Open Source License

Parameter

Parameter Description
date the given date as string

Exception

Parameter Description
ParseException if <code>date</code> string can not be parsed.

Return

the java.util.Date.

Declaration

public static Date parse(String date) throws ParseException 

Method Source Code


//package com.java2s;
/*/*www  . ja va 2 s .c om*/
 * Utility class for a easy way to handle Date
 * Copyright (C) 2012 Martin Absmeier, IT Consulting Services
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

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

import java.util.Date;

public class Main {
    /** GERMAN_STANDARD_NO_TIME - dd.MM.yyyy */
    public static DateFormat GERMAN_STANDARD_NO_TIME = new SimpleDateFormat("dd.MM.yyyy");
    /** GERMAN_SHORT_NO_TIME - dd.MM.yy */
    public static DateFormat GERMAN_SHORT_NO_TIME = new SimpleDateFormat("dd.MM.yy");
    /** US_STANDARD_NO_TIME - yyyy-MM-dd */
    public static DateFormat US_STANDARD_NO_TIME = new SimpleDateFormat("yyyy-MM-dd");
    /** US_SHORT_NO_TIME - yy-MM-dd */
    public static DateFormat US_SHORT_NO_TIME = new SimpleDateFormat("yy-MM-dd");

    /**
     * Analyzes the <code>date</code> string and tries to generate a
     * <code>java.util.Date</code>.
     * 
     * @param date
     *            the given date as string
     * @return the <code>java.util.Date</code>.
     * @throws ParseException
     *             if <code>date</code> string can not be parsed.
     */
    public static Date parse(String date) throws ParseException {
        Date result = null;

        // dd.MM.yy or yy-MM-dd
        if (date.length() == 8) {
            if (date.contains(".")) {
                result = GERMAN_SHORT_NO_TIME.parse(date);
            } else {
                result = US_SHORT_NO_TIME.parse(date);
            }
        }

        // dd.MM.yyyy or yyyy-MM-dd
        if (date.length() == 10) {
            if (date.contains(".")) {
                result = GERMAN_STANDARD_NO_TIME.parse(date);
            } else {
                result = US_STANDARD_NO_TIME.parse(date);
            }
        }

        return result;
    }
}

Related

  1. parse(String date)
  2. parse(String date)
  3. parse(String date)
  4. parse(String date)
  5. parse(String date)
  6. parse(String date)
  7. parse(String date)
  8. parse(String date)
  9. parse(String date)