Java Parse HTTP Date parseHttpDate(String dstr)

Here you can find the source of parseHttpDate(String dstr)

Description

Parse the http date string.

License

Open Source License

Parameter

Parameter Description
dstr the date string to parse

Return

the Date object

Declaration

final static Date parseHttpDate(String dstr) 

Method Source Code

//package com.java2s;
/*/*from  w w w. ja  v a  2s  .c  om*/
 * @(#)Util.java               0.3-3 06/05/2001
 *
 *  This file is part of the HTTPClient package
 *  Copyright (C) 1996-2001 Ronald Tschalar
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA 02111-1307, USA
 *
 *  For questions, suggestions, bug-reports, enhancement-requests etc.
 *  I may be contacted at:
 *
 *  ronald@innovation.ch
 *
 *  The HTTPClient's home page is located at:
 *
 *  http://www.innovation.ch/java/HTTPClient/ 
 *
 */

import java.util.Date;

import java.util.Locale;

import java.util.SimpleTimeZone;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class Main {
    private static DateFormat parse_1123;
    private static DateFormat parse_850;
    private static DateFormat parse_asctime;
    private static final Object http_parse_lock = new Object();

    /**
     * Parse the http date string. java.util.Date will do this fine, but
     * is deprecated, so we use SimpleDateFormat instead.
     *
     * @param dstr  the date string to parse
     * @return the Date object
     */
    final static Date parseHttpDate(String dstr) {
        synchronized (http_parse_lock) {
            if (parse_1123 == null)
                setupParsers();
        }

        try {
            return parse_1123.parse(dstr);
        } catch (java.text.ParseException pe) {
        }
        try {
            return parse_850.parse(dstr);
        } catch (java.text.ParseException pe) {
        }
        try {
            return parse_asctime.parse(dstr);
        } catch (java.text.ParseException pe) {
            throw new IllegalArgumentException(pe.toString());
        }
    }

    private static final void setupParsers() {
        parse_1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
        parse_850 = new SimpleDateFormat("EEEE, dd-MMM-yy HH:mm:ss 'GMT'", Locale.US);
        parse_asctime = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy", Locale.US);

        parse_1123.setTimeZone(new SimpleTimeZone(0, "GMT"));
        parse_850.setTimeZone(new SimpleTimeZone(0, "GMT"));
        parse_asctime.setTimeZone(new SimpleTimeZone(0, "GMT"));

        parse_1123.setLenient(true);
        parse_850.setLenient(true);
        parse_asctime.setLenient(true);
    }
}

Related

  1. parseHttpDate(String _dateStr)
  2. parseHttpDate(String ifModifiedSince)
  3. parseHttpDate(String s)
  4. parseHttpDate(String string)
  5. parseHttpDate(String stringValue)