Java Parse Date Pattern YYYY parseStringForDate(String dateString)

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

Description

parse String For Date

License

Open Source License

Declaration

private static Date parseStringForDate(String dateString) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import java.util.Date;

public class Main {
    private static Date parseStringForDate(String dateString) {
        //Since there is no way to know the TimeZone of the document, will assume the time zone of the end-user.

        String[] dateFormats = new String[] { "yyyy-MM-dd'T'HH:mm",
                "yyyy-MM-dd" };

        for (int i = 0; i < dateFormats.length; i++) {
            Date date = attemptParse(dateString, dateFormats[i]);
            if (date != null) {
                return date;
            }//from  w  w w .  j a  v  a2  s . c om
        }
        return null;
    }

    /**
     * An attempt to parse the dateString. Will return null if it cannot be parsed.
     * @param dateString
     * @param dateFormat
     * @return
     */
    private static Date attemptParse(String dateString, String dateFormat) {
        try {
            SimpleDateFormat format;
            if (dateFormat != null) {
                format = new SimpleDateFormat(dateFormat);
            } else {
                format = new SimpleDateFormat();
            }
            Date parsedDate = format.parse(dateString);
            return parsedDate;
        } catch (ParseException e) {
            ;
        }

        return null;
    }
}

Related

  1. parseString2Date(String sDate, String format)
  2. parseString2Long(String strDate)
  3. parseStringAsDate(String pattern, String value)
  4. parseStringDate(String dateAsString)
  5. parseStringDateToRmFormat(String date)
  6. parseStringToCalendar(String strDate)
  7. parseStringToDate(final String pDate, final String... pFormat)
  8. parseStringToDate(String date)
  9. parseStringToDate(String dateString)