Java Parse Date Pattern YYYY parse(String text)

Here you can find the source of parse(String text)

Description

This method tries several different formats to parsing a date.

License

Open Source License

Parameter

Parameter Description
text the date time string to parse

Return

the Date reference if parsing was successful or null if the text could not be parsed into a date.

Declaration

public static Date parse(String text) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w  .  j  a  va 2  s.  c o m*/
 * Copyright (c) 2004 Stephan D. Cote' - All rights reserved.
 * 
 * This program and the accompanying materials are made available under the 
 * terms of the MIT License which accompanies this distribution, and is 
 * available at http://creativecommons.org/licenses/MIT/
 *
 * Contributors:
 *   Stephan D. Cote 
 *      - Initial API and implementation
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;

import java.util.Date;

import java.util.List;

public class Main {
    private static final List<String> formatStrings = Arrays.asList("yyyy-MM-dd'T'HH:mm:ss.SSSX",
            "yyyy-MM-dd'T'HH:mm:ss.SSS", "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd HH:mm:ss.SSSX",
            "yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "MM-dd-yyyy HH:mm:ss.SSSX",
            "MM-dd-yyyy HH:mm:ss.SSS", "MM-dd-yyyy HH:mm:ss", "MM/dd/yyyy HH:mm:ss", "EEE MMM dd HH:mm:ss zzz yyyy",
            "HH:mm:ss.SSSX", "HH:mm:ss.SSS", "HH:mm:ss", "HH:mm", "M/y", "M-y", "M/d/y", "M-d-y", "y/M/d", "y-M-d");

    /**
     * This method tries several different formats to parsing a date.
     * 
     * <p>This method is useful if the actual format of the date is not known.
     * 
     * @param text the date time string to parse
     * 
     * @return the Date reference if parsing was successful or null if the text 
     *         could not be parsed into a date.
     */
    public static Date parse(String text) {
        for (String formatString : formatStrings) {
            try {
                return new SimpleDateFormat(formatString).parse(text);
            } catch (ParseException e) {
            }
        }
        return null;
    }
}

Related

  1. parse(String strDate)
  2. parse(String string)
  3. parse(String string)
  4. parse(String stringData)
  5. parse(String text)
  6. parse(String time)
  7. parse(String time, String df)
  8. parse1(String DateString, String dateTimeSeparator)
  9. parse2DateString(String date)