Java Parse Date parseDate(String value)

Here you can find the source of parseDate(String value)

Description

parse Date

License

Open Source License

Declaration

public static Date parseDate(String value) 

Method Source Code


//package com.java2s;
/*/*from  w  ww  .ja v a2s.  c  om*/
 * (c) Copyright 2010-2011 AgileBirds
 *
 * This file is part of OpenFlexo.
 *
 * OpenFlexo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * OpenFlexo 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenFlexo. If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Main {
    private static final String[] ACCEPTABLEDATEFORMAT = { "d/M/y", "d/M/y H'h'm", "d/M/y H:m", "d-M-y",
            "d-M-y H'h'm", "d-M-y H:m", "M/d/y", "M/d/y H'h'm", "M/d/y H:m", "M-d-y", "M-d-y H'h'm", "M-d-y H:m" };

    /**
     * Parse all specified String values and returns them as a date array. This method try to detect the date format to use and ensure the same will be used for all values. <br>
     * If a common date format cannot be found, null is returned. 
     * 
     * @param values
     * @return all specified String values as Date, null if they cannot be converted.
     */
    public static Date[] parseDate(String[] values) {
        SimpleDateFormat dateFormat = new SimpleDateFormat();
        dateFormat.setLenient(false);
        ParsePosition pos = new ParsePosition(0);
        for (String pattern : ACCEPTABLEDATEFORMAT) {
            dateFormat.applyPattern(pattern);
            List<Date> results = new ArrayList<Date>();

            for (String value : values) {
                pos.setIndex(0);
                Date date = dateFormat.parse(value.trim(), pos);
                if (date == null || pos.getIndex() != value.length())
                    break;
                results.add(date);
            }

            if (results.size() == values.length)
                return results.toArray(new Date[0]);
        }

        return null;
    }

    public static Date parseDate(String value) {
        Date[] dates = parseDate(new String[] { value });
        if (dates != null && dates.length > 0)
            return dates[0];
        return null;
    }
}

Related

  1. parseDate(String time)
  2. parseDate(String timeString)
  3. parseDate(String token)
  4. parseDate(String value)
  5. parseDate(String value)
  6. parseDate(String value)
  7. parseDate(String value)
  8. parseDate(String value)
  9. parseDate(String value, Class targetType, String... formats)