Android Date String Parse parseDate(String atomPubDate)

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

Description

parse Date

License

Apache License

Declaration

public static Date parseDate(String atomPubDate) 

Method Source Code

/*******************************************************************************
 * Copyright (C) 2005-2012 Alfresco Software Limited.
 * // w ww  .  j  ava  2  s. c om
 * This file is part of the Alfresco Mobile SDK.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 ******************************************************************************/

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class Main{
    private static final String[] DATE_FORMATS = { FORMAT_1, FORMAT_2,
            FORMAT_3, FORMAT_4, FORMAT_5 };
    public static Date parseDate(String atomPubDate) {
        return parseDate(atomPubDate, Locale.getDefault());
    }
    /**
     * @since 1.0.1
     * @param atomPubDate
     * @return
     */
    public static Date parseDate(String atomPubDate, Locale locale) {
        Date d = null;
        SimpleDateFormat sdf;
        for (int i = 0; i < DATE_FORMATS.length; i++) {
            sdf = new SimpleDateFormat(DATE_FORMATS[i], locale);
            sdf.setLenient(true);
            try {
                d = sdf.parse(atomPubDate);
                break;
            } catch (ParseException e) {
                continue;
            }
        }

        if (d == null) {
            for (int i = 0; i < DATE_FORMATS.length; i++) {
                sdf = new SimpleDateFormat(DATE_FORMATS[i], Locale.ENGLISH);
                sdf.setLenient(true);
                try {
                    d = sdf.parse(atomPubDate);
                    break;
                } catch (ParseException e) {
                    continue;
                }
            }
        }

        return d;
    }
    public static Date parseDate(String date, String format) {
        if (date == null) {
            return null;
        }
        return parseDate(date,
                new SimpleDateFormat(format, Locale.getDefault()));
    }
    /**
     * @since 1.0.1
     * @param atomPubDate
     * @return
     */
    public static Date parseDate(String date, SimpleDateFormat sdf) {
        Date d = null;
        sdf.setLenient(true);
        try {
            d = sdf.parse(date);
        } catch (ParseException e) {
            d = parseDate(date);
        }
        return d;
    }
}

Related

  1. parse(String date, boolean isDateOnly)
  2. parse(String dateString, String dateFormat)
  3. parse(String src)
  4. parseDA(String s, boolean end)
  5. parseDT(String s, boolean end)
  6. parseDate(String atomPubDate, Locale locale)
  7. parseDate(String date)
  8. parseDate(String date, SimpleDateFormat sdf)
  9. parseDate(String date, String format)