Android Date String Parse parseRFC3339Date(String date)

Here you can find the source of parseRFC3339Date(String date)

Description

parse RFC Date

License

Open Source License

Declaration

public static Date parseRFC3339Date(String date) 

Method Source Code

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

import android.util.Log;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    private static final String TAG = "DateUtils";
    /**//from   www. j ava  2s.  c  o m
     * RFC 3339 date format for UTC dates.
     */
    public static final String RFC3339UTC = "yyyy-MM-dd'T'HH:mm:ss'Z'";
    /**
     * RFC 3339 date format for localtime dates with offset.
     */
    public static final String RFC3339LOCAL = "yyyy-MM-dd'T'HH:mm:ssZ";
    private static ThreadLocal<SimpleDateFormat> RFC3339Formatter = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat(RFC3339UTC, Locale.US);
        }

    };

    public static Date parseRFC3339Date(String date) {
        Date result = null;
        SimpleDateFormat format = RFC3339Formatter.get();
        boolean isLocal = date.endsWith("Z");
        if (date.contains(".")) {
            // remove secfrac
            int fracIndex = date.indexOf(".");
            String first = date.substring(0, fracIndex);
            String second = null;
            if (isLocal) {
                second = date.substring(date.length() - 1);
            } else {
                if (date.contains("+")) {
                    second = date.substring(date.indexOf("+"));
                } else {
                    second = date.substring(date.indexOf("-"));
                }
            }

            date = first + second;
        }
        if (isLocal) {
            try {
                result = format.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            format.applyPattern(RFC3339LOCAL);
            // remove last colon
            StringBuffer buf = new StringBuffer(date.length() - 1);
            int colonIdx = date.lastIndexOf(':');
            for (int x = 0; x < date.length(); x++) {
                if (x != colonIdx)
                    buf.append(date.charAt(x));
            }
            String bufStr = buf.toString();
            try {
                result = format.parse(bufStr);
            } catch (ParseException e) {
                e.printStackTrace();
                Log.e(TAG, "Unable to parse date");
            } finally {
                format.applyPattern(RFC3339UTC);
            }

        }

        return result;

    }
}

Related

  1. convertDateTime( String YearMonthDayHourMinuteSeconds)
  2. convertToDate(String dateString)
  3. getDateFromString(String format, String dateString)
  4. getDateFromLJString( String dateString)
  5. getDateFromString(String dateString)
  6. parseRFC822Date(String date)
  7. parseIcalDateToString(Date date, TimeZone tz)
  8. parseDate(String s)
  9. parseDate(String value)