Java Parse Date parseDate(String date)

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

Description

parse a date

License

Open Source License

Parameter

Parameter Description
date a parameter

Exception

Parameter Description
ParseException an exception

Declaration

private static Date parseDate(String date) throws ParseException 

Method Source Code

//package com.java2s;
/*//w w w  .  j  a  v  a 2 s. c o  m
 * Copyright (c) 2009 The Regents of the University of California.
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the above
 * copyright notice and the following two paragraphs appear in all copies
 * of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 * ENHANCEMENTS, OR MODIFICATIONS.
 *
 */

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

public class Main {
    /**
     * parse a date
     *
     * @param date
     * @return
     * @throws ParseException
     */
    private static Date parseDate(String date) throws ParseException {
        Date result = null;
        if (date.matches("\\d\\d\\d\\d-\\d(\\d)?-\\d(\\d)?")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            result = sdf.parse(date);
        } else if (date.matches("\\d(\\d)?-\\d(\\d)?-\\d\\d(\\d\\d)?")) {
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yy");
            result = sdf.parse(date);
        } else if (date.matches("\\d(\\d)?-\\d(\\d)?")) {
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yy");
            result = sdf.parse(date + "-"
                    + Calendar.getInstance().get(Calendar.YEAR));
        } else if (date
                .matches("[a-zA-Z]+\\s+\\d(\\d)?,\\s+\\d\\d(\\d\\d)?")) {
            SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, yy");
            result = sdf.parse(date);
        } else if (date.matches("[a-zA-Z]+\\s+\\d(\\d)?")) {
            SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, yy");
            result = sdf.parse(date + ", "
                    + Calendar.getInstance().get(Calendar.YEAR));
        } else if (date.matches("\\d(\\d)?\\s+[a-zA-Z]+\\s+\\d\\d\\d\\d")) {
            SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
            result = sdf.parse(date);
        } else if (date.matches("\\d\\d\\d\\d\\s+[a-zA-Z]+\\s+\\d(\\d)?")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMMM dd");
            result = sdf.parse(date);
        } else if (date
                .matches("\\d(\\d)?[a-zA-Z][a-zA-Z][a-zA-Z]\\d\\d\\d\\d")) {
            SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyy");
            result = sdf.parse(date);
        } else if (date
                .matches("\\d\\d\\d\\d[a-zA-Z][a-zA-Z][a-zA-Z]\\d(\\d)?")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMMdd");
            result = sdf.parse(date);
        } else {
            throw new ParseException("", 0);
        }
        return result;
    }
}

Related

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