All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.asynchttpclient.cookie.DateParser Maven / Gradle / Ivy

/*
 * Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */
package org.asynchttpclient.cookie;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * A parser for RFC2616
 * Date format.
 * 
 * @author slandelle
 */
@SuppressWarnings("serial")
public class DateParser extends SimpleDateFormat {

    private final SimpleDateFormat format1 = new RFC2616DateParserObsolete1();
    private final SimpleDateFormat format2 = new RFC2616DateParserObsolete2();

    private static final ThreadLocal DATE_FORMAT_HOLDER = new ThreadLocal() {
        @Override
        protected DateParser initialValue() {
            return new DateParser();
        }
    };

    public static DateParser get() {
        return DATE_FORMAT_HOLDER.get();
    }

    /**
     * Standard date format
     * 
* E, d MMM yyyy HH:mm:ss z * e.g. Sun, 06 Nov 1994 08:49:37 GMT */ private DateParser() { super("E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH); setTimeZone(TimeZone.getTimeZone("GMT")); } @Override public Date parse(String text, ParsePosition pos) { Date date = super.parse(text, pos); if (date == null) { date = format1.parse(text, pos); } if (date == null) { date = format2.parse(text, pos); } return date; } /** * First obsolete format *
* E, d-MMM-y HH:mm:ss z * e.g. Sunday, 06-Nov-94 08:49:37 GMT */ private static final class RFC2616DateParserObsolete1 extends SimpleDateFormat { private static final long serialVersionUID = -3178072504225114298L; RFC2616DateParserObsolete1() { super("E, dd-MMM-yy HH:mm:ss z", Locale.ENGLISH); setTimeZone(TimeZone.getTimeZone("GMT")); } } /** * Second obsolete format *
* EEE, MMM d HH:mm:ss yyyy * e.g. Sun Nov 6 08:49:37 1994 */ private static final class RFC2616DateParserObsolete2 extends SimpleDateFormat { private static final long serialVersionUID = 3010674519968303714L; RFC2616DateParserObsolete2() { super("E MMM d HH:mm:ss yyyy", Locale.ENGLISH); setTimeZone(TimeZone.getTimeZone("GMT")); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy