Android Date String Parse parseJsonDate(String jsonDate)

Here you can find the source of parseJsonDate(String jsonDate)

Description

parse Json Date

License

Apache License

Declaration

public static Date parseJsonDate(String jsonDate) 

Method Source Code

/*******************************************************************************
 * Copyright (C) 2005-2012 Alfresco Software Limited.
 * /*  w  ww.jav  a2s  .  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{
    public static final String FORMAT_4 = "MMM dd yyyy HH:mm:ss zzzz";
    private static final String[] DATE_FORMATS = { FORMAT_1, FORMAT_2,
            FORMAT_3, FORMAT_4, FORMAT_5 };
    public static Date parseJsonDate(String jsonDate) {
        Date d = null;
        if (jsonDate != null) {
            d = parseDate(jsonDate, FORMAT_4);
            if (d == null) {
                d = parseDate(jsonDate);
            }
        }
        return d;
    }
    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. parseDate(String date)
  2. parse(String dateStr)
  3. parse(String input)
  4. parse(String source, String pattern)
  5. parseDate(String value, String[] parsePatterns)
  6. str2Date(String str)
  7. stringConvertDate(String date)
  8. stringToDate(String date)
  9. toDate(String string)