Java Parse RFC Date parseRFC3339Date(String datestring)

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

Description

To deal with RFC3339 Date

License

Open Source License

Parameter

Parameter Description
datestring datestring

Exception

Parameter Description

Return

Date

Declaration

public static Date parseRFC3339Date(String datestring) throws java.text.ParseException 

Method Source Code

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

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    public static final String RFC3399_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";

    /**//from  ww w . j a v  a 2s .c o m
     * To deal with RFC3339 Date
     * 
     * @param datestring datestring
     * @return Date
     * @throws java.text.ParseException in case of error
     */
    public static Date parseRFC3339Date(String datestring) throws java.text.ParseException {
        Date d = new Date();
        // if there is no time zone, we don't need to do any special parsing.
        if (datestring.endsWith("Z")) {
            SimpleDateFormat s = new SimpleDateFormat(RFC3399_DATE_FORMAT);// spec for RFC3339
            s.setTimeZone(TimeZone.getTimeZone("UTC"));
            try {
                d = s.parse(datestring);
            } catch (java.text.ParseException pe) {// try again with optional decimals
                s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
                s.setTimeZone(TimeZone.getTimeZone("UTC"));
                s.setLenient(true);
                d = s.parse(datestring);
            }
            return d;
        }

        // step one, split off the timezone.
        char timezoneSeparator;
        if (datestring.contains("+")) {
            timezoneSeparator = '+';
        } else {
            timezoneSeparator = '-';
        }

        String firstpart = datestring.substring(0, datestring.lastIndexOf(timezoneSeparator));
        String secondpart = datestring.substring(datestring.lastIndexOf(timezoneSeparator));

        // step two, remove the colon from the timezone offset
        secondpart = secondpart.substring(0, secondpart.indexOf(':'))
                + secondpart.substring(secondpart.indexOf(':') + 1);
        datestring = firstpart + secondpart;
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");// spec for RFC3339
        s.setTimeZone(TimeZone.getTimeZone("UTC"));
        try {
            d = s.parse(datestring);
        } catch (java.text.ParseException pe) {// try again with optional decimals
            s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");// spec for RFC3339 (with fractional seconds)
            s.setTimeZone(TimeZone.getTimeZone("UTC"));
            s.setLenient(true);
            d = s.parse(datestring);
        }
        return d;
    }
}

Related

  1. parseRFC1123(String string)
  2. parseRFC2822Date(String encoded, Date fallback)
  3. parseRfc2822DateToString(final Date rfcDate)
  4. parseRfc3339Calendar(String value)
  5. parseRfc3339Date(String date)
  6. parseRFC3339Date(String datestring)
  7. parseRFC822(String dateString)
  8. parseRFC822(String sDate, Locale locale)
  9. parseRFC822(String str)