Java Date Format Change convertDate(String date, String[] formats)

Here you can find the source of convertDate(String date, String[] formats)

Description

This method try to parse a string into a Date object using an array with the valid formats

License

Open Source License

Parameter

Parameter Description
date the string to be parsed
formats the valid format to parse the string

Exception

Parameter Description

Return

return the Date object that represent the string

Declaration


public static Date convertDate(String date, String[] formats) throws java.text.ParseException 

Method Source Code


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

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**//from  ww w.j a va 2  s . com
     * This method try to parse a string into a Date object using an array with
     * the valid formats
     * 
     * @param date
     *            the string to be parsed
     * @param formats
     *            the valid format to parse the string
     * @return return the Date object that represent the string
     * @throws java.text.ParseException
     */

    public static Date convertDate(String date, String[] formats) throws java.text.ParseException {
        Date ret = null;
        for (String pattern : formats) {
            try {
                ret = new SimpleDateFormat(pattern).parse(date);
                break;
            } catch (java.text.ParseException e) {
            }
        }
        if (ret == null)
            throw new java.text.ParseException(date, 0);

        return ret;
    }
}

Related

  1. changeDateFormat(String dateString, String srcFormat, String destFormat)
  2. changeDateFormatPattern(final DateFormat dateFormat, String regex, String replacement)
  3. changeDateTimeZone(String date_GMT)
  4. changeDateValue(Date firstDate, Date secondDate)
  5. convertDate(String date, String sourceFormat, String destinationFormat)
  6. convertDate(String dateIn, String fromDateFormat, String toDateFormat)
  7. convertDate(String inPattern, String outPattern, String date)
  8. convertDate(String inPattern, String outPattern, String date)
  9. convertDate(String str)