Java Parse RFC Date parseRFC3339Date(String datestring)

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

Description

parse RFC Date

License

Apache License

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Software Name : RCS IMS Stack/*from   w w  w.jav a  2s.c om*/
 *
 * Copyright (C) 2010 France Telecom S.A.
 *
 * 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.SimpleDateFormat;
import java.util.Date;

public class Main {
    static Date parseRFC3339Date(String datestring) throws java.text.ParseException, IndexOutOfBoundsException {
        Date d = new Date();

        //if there is no time zone, we don't need to do any special parsing.
        if (datestring.endsWith("Z")) {
            try {
                SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");//spec for RFC3339               
                d = s.parse(datestring);
            } catch (java.text.ParseException pe) {//try again with optional decimals
                SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");//spec for RFC3339 (with fractional seconds)
                s.setLenient(true);
                d = s.parse(datestring);
            }
            return d;
        }

        //step one, split off the timezone. 
        String firstpart = datestring.substring(0, datestring.lastIndexOf('-'));
        String secondpart = datestring.substring(datestring.lastIndexOf('-'));

        //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      
        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.setLenient(true);
            d = s.parse(datestring);
        }
        return d;
    }
}

Related

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