Android Date String Parse parseDate(String date, SimpleDateFormat sdf)

Here you can find the source of parseDate(String date, SimpleDateFormat sdf)

Description

parse Date

License

Apache License

Parameter

Parameter Description
atomPubDate a parameter

Declaration

public static Date parseDate(String date, SimpleDateFormat sdf) 

Method Source Code

/*******************************************************************************
 * Copyright (C) 2005-2012 Alfresco Software Limited.
 * //from  w  w  w  .j av  a  2s. c o  m
 * 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. parseDA(String s, boolean end)
  2. parseDT(String s, boolean end)
  3. parseDate(String atomPubDate)
  4. parseDate(String atomPubDate, Locale locale)
  5. parseDate(String date)
  6. parseDate(String date, String format)
  7. parseDate(String date, String pattern)
  8. parseDate(String date, String pattern)
  9. parseDate(String date, String pattern, TimeZone zone)