Java Parse Date parseDateTime(String dateTime)

Here you can find the source of parseDateTime(String dateTime)

Description

return a date from a string

License

Open Source License

Parameter

Parameter Description
dateTime a parameter

Exception

Parameter Description
ParseException an exception

Declaration

public static Date parseDateTime(String dateTime) throws ParseException 

Method Source Code

//package com.java2s;
/*/*  ww  w .  j  a v a2s.  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 {
    /**
     * return a date from a string
     *
     * @param dateTime
     * @return
     * @throws ParseException
     */
    public static Date parseDateTime(String dateTime) throws ParseException {
        Calendar time = Calendar.getInstance();
        time.set(0, 0, 0, 0, 0);
        String rest = "";
        for (String part : dateTime.split("\\s")) {
            if (part.contains(":")) {
                time = parseTime(time, part);
            } else {
                rest += part + " ";
            }
        }
        rest = rest.trim();
        Date date = parseDate(rest);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
        SimpleDateFormat dateTimeFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm");

        return dateTimeFormat.parse(dateFormat.format(date) + " "
                + timeFormat.format(time.getTime()));
    }

    /**
     * return a calendar from a cal and time
     *
     * @param c
     * @param time
     * @return
     * @throws ParseException
     */
    private static Calendar parseTime(Calendar c, String time)
            throws ParseException {
        if (time.matches("\\d(\\d)?:\\d(\\d)?")) {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            c.setTime(sdf.parse(time));
            return c;
        } else {
            return null;
        }
    }

    /**
     * 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. parseDateTime(String currDate, String format)
  2. parseDateTime(String d, String t)
  3. parseDateTime(String date)
  4. parseDateTime(String date, String format, String locale, String timeZone)
  5. parseDateTime(String date, String time)
  6. parseDateTime(String dateTimeStr)
  7. parseDateTime(String dateTimeString)
  8. parseDateTime(String dateTimeString)
  9. parseDateTime(String dateTimeString, String[] validFormats, String outputFormat)