Java Parse RFC Date parseRfc3339Calendar(String value)

Here you can find the source of parseRfc3339Calendar(String value)

Description

Parses a datetime+timezone string in one of the following formats, returning a calendar in the given timezone.

License

Apache License

Parameter

Parameter Description
date string

Declaration

public static Calendar parseRfc3339Calendar(String value) throws ParseException 

Method Source Code


//package com.java2s;
/*//w w w . ja  v  a2  s. c  o  m
 * Copyright 2006 Open Source Applications Foundation
 * 
 * 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.Calendar;
import java.util.GregorianCalendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    private static final String RFC3339_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssz";

    /**
     * Parses a datetime+timezone string in one of the following
     * formats, returning a calendar in the given timezone.
     * 
     * 2002-10-10T00:00:00+05:00
     * 2002-10-09T19:00:00Z
     * 2002-10-10T00:00:00GMT+05:00
     * 
     * @param date string
     */
    public static Calendar parseRfc3339Calendar(String value) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(RFC3339_DATE_FORMAT);
        TimeZone timezone = null;
        Date date = null;
        if (value.charAt(value.length() - 1) == 'Z') {
            value = value.replace("Z", "GMT-00:00");
            timezone = TimeZone.getTimeZone("GMT-00:00");
            date = sdf.parse(value);
        } else if (value.indexOf("GMT") == -1
                && (value.charAt(value.length() - 6) == '+' || value.charAt(value.length() - 6) == '-')) {
            String tzId = "GMT" + value.substring(value.length() - 6);
            value = value.substring(0, value.length() - 6) + tzId;
            timezone = TimeZone.getTimeZone(tzId);
            date = sdf.parse(value);
        } else {
            String tzId = value.substring(value.length() - 9);
            timezone = TimeZone.getTimeZone(tzId);
            date = sdf.parse(value);
        }

        GregorianCalendar cal = new GregorianCalendar(timezone);
        cal.setTime(date);

        return cal;
    }
}

Related

  1. parse(String rfc1123Date)
  2. parseRFC1123(String string)
  3. parseRFC2822Date(String encoded, Date fallback)
  4. parseRfc2822DateToString(final Date rfcDate)
  5. parseRfc3339Date(String date)
  6. parseRFC3339Date(String datestring)
  7. parseRFC3339Date(String datestring)
  8. parseRFC822(String dateString)