Java Parse Date Pattern YYYY parseStringToDate(String dateString)

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

Description

parse String To Date

License

Open Source License

Declaration

public static java.util.Date parseStringToDate(String dateString) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008 Ariadne Foundation.
 * //ww  w .  j a v a 2s . c  om
 * This file is part of Ariadne Harvester.
 * 
 * Ariadne Harvester 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 3 of the License, or
 * (at your option) any later version.
 * 
 * Ariadne Harvester 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 Ariadne Harvester.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.text.ParseException;

public class Main {
    public static java.util.Date parseStringToDate(String dateString) {
        java.text.SimpleDateFormat dfparser;
        dateString = dateString.replaceAll("th", "");
        dateString = dateString.replaceAll("nd", "");
        dateString = dateString.replaceAll("\\n", " ");
        dateString = dateString.replaceAll("\\r", " ");
        dateString = dateString.replaceAll(
                System.getProperty("line.separator"), " ");
        // Replace 2 or more spaces by 1 space, because otherwise the parsing
        // will fail.
        dateString = dateString.replaceAll("\\s{2,}", " ");
        try {
            // The one used by the LOM Java API
            dfparser = new java.text.SimpleDateFormat(
                    "EEE, dd MMM yyyy HH:mm:ss z", java.util.Locale.US);
            return dfparser.parse(dateString);
        } catch (ParseException e) {
            //
        }
        try {
            // The one used by java.util.Date toString method
            dfparser = new java.text.SimpleDateFormat(
                    "EEE MMM dd HH:mm:ss zzz yyyy", java.util.Locale.US);
            return dfparser.parse(dateString);
        } catch (ParseException e) {
            //
        }
        try {
            dfparser = new java.text.SimpleDateFormat(
                    "yyyy-MM-dd'T'HH:mm:ss'Z'");
            return dfparser.parse(dateString);
        } catch (ParseException e) {
            //
        }
        try {
            dfparser = new java.text.SimpleDateFormat("yyyy-MM-dd");
            return dfparser.parse(dateString);
        } catch (ParseException e) {
            //
        }
        try {
            dfparser = new java.text.SimpleDateFormat("MMM dd, yyyy");
            return dfparser.parse(dateString);
        } catch (ParseException e) {
            //
        }
        try {
            dfparser = new java.text.SimpleDateFormat("MM-dd.yy");
            return dfparser.parse(dateString);
        } catch (ParseException e) {
            //
        }
        try {
            dfparser = new java.text.SimpleDateFormat("dd MMM yyyy");
            return dfparser.parse(dateString);
        } catch (ParseException e) {
            //
        }
        try {
            dfparser = new java.text.SimpleDateFormat("dd.MM.yy");
            return dfparser.parse(dateString);
        } catch (ParseException e) {
            //
        }
        try {
            dfparser = new java.text.SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
            return dfparser.parse(dateString);
        } catch (ParseException e) {
            //
        }
        try {
            dfparser = new java.text.SimpleDateFormat(
                    "E M dd hh:mm:ss z yyyy");
            return dfparser.parse(dateString);
        } catch (ParseException e) {
            //
        }
        // If all attempts fail, just return null.
        return null;
    }
}

Related

  1. parseStringDateToRmFormat(String date)
  2. parseStringForDate(String dateString)
  3. parseStringToCalendar(String strDate)
  4. parseStringToDate(final String pDate, final String... pFormat)
  5. parseStringToDate(String date)
  6. parseStringToDatePrettyPrint(Date date)
  7. parseStringToUtilDate(String strDate)
  8. parseTIPPDate(String dateString)
  9. parseToDate(final String date, final String format)