Java Parse Date parseDate(final String dateString)

Here you can find the source of parseDate(final String dateString)

Description

This method parses a Date from a string with the format dd.MM.yyyy kk:mm.

License

Open Source License

Parameter

Parameter Description
dateString a parameter

Declaration

public static Date parseDate(final String dateString) 

Method Source Code

//package com.java2s;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    /**/*w w  w  . ja  v  a2  s  . c o m*/
     * This method parses a {@link Date} from a string with the format
     * dd.MM.yyyy kk:mm.
     * 
     * @param dateString
     * @return
     */
    public static Date parseDate(final String dateString) {
        Date date = null;
        DateFormat formatter;
        try {
            formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
            date = formatter.parse(dateString);
        } catch (final ParseException d) {
            try {
                formatter = new SimpleDateFormat(
                        "yyyy-MM-dd'T'HH:mm:ss.SSSZ");
                date = formatter.parse(dateString);
            } catch (final ParseException e) {
                try {
                    formatter = new SimpleDateFormat(
                            "yyyy-MM-dd'T'HH:mm:ss.SSS");
                    date = formatter.parse(dateString);
                } catch (final ParseException f) {
                    try {
                        formatter = new SimpleDateFormat(
                                "yyyy-MM-dd'T'HH:mm:ss");
                        date = formatter.parse(dateString);
                    } catch (final ParseException g) {
                        try {
                            formatter = new SimpleDateFormat(
                                    "yyyy-MM-dd' 'HH:mm:ss");
                            date = formatter.parse(dateString);
                        } catch (final ParseException h) {
                            try {
                                formatter = new SimpleDateFormat(
                                        "dd-MM-yyyy' 'HH:mm:ss");
                                date = formatter.parse(dateString);
                            } catch (final ParseException i) {
                                try {
                                    formatter = new SimpleDateFormat(
                                            "yyyy-MM-dd");
                                    date = formatter.parse(dateString);
                                } catch (final ParseException j) {
                                    try {
                                        formatter = new SimpleDateFormat(
                                                "dd.MM.yyyy kk:mm");
                                        date = formatter.parse(dateString);

                                    } catch (final ParseException k) {
                                        try {
                                            // Aug 6, 2014 3:52:29 PM
                                            formatter = new SimpleDateFormat(
                                                    "MMM d, yyyy h:mm:ss a");
                                            date = formatter
                                                    .parse(dateString);
                                        } catch (final ParseException l) {
                                            try {
                                                // 2013-07-14T23:30:59+02:00
                                                formatter = new SimpleDateFormat(
                                                        "yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
                                                date = formatter
                                                        .parse(dateString);
                                            } catch (final ParseException m) {
                                                // System.out.println("hpi.eap.DateUtils - date not parseable: "
                                                // + dateString);
                                                try {
                                                    long longDate = Long
                                                            .parseLong(dateString);
                                                    date = new Date(
                                                            longDate * 1000);
                                                } catch (final NumberFormatException n) {
                                                    n.printStackTrace();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return date;
    }
}

Related

  1. parseDate(final String date)
  2. parseDate(final String date)
  3. parseDate(final String date, final List patterns)
  4. parseDate(final String date, final String format)
  5. parseDate(final String dateStr)
  6. parseDate(final String pDate)
  7. parseDate(final String revision)
  8. parseDate(final String source)
  9. parseDate(final String source)