Java Parse RFC Date parseRFC822Date(String value)

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

Description

parse RFC Date

License

Apache License

Declaration

public static Date parseRFC822Date(String value) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Jack Wang/*from  w  w  w  .  j  a  v  a 2  s . c o  m*/
 * 
 * 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.Date;

public class Main {
    public static final SimpleDateFormat rfc822DateFormats[] = new SimpleDateFormat[] {
            new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z"), new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"),
            new SimpleDateFormat("EEE, d MMM yy HH:mm:ss z"), new SimpleDateFormat("EEE, d MMM yyyy HH:mm z"),
            new SimpleDateFormat("EEE, d MMM yy HH:mm z"), new SimpleDateFormat("d MMM yyyy HH:mm:ss z"),
            new SimpleDateFormat("d MMM yy HH:mm:ss z"), new SimpleDateFormat("d MMM yyyy HH:mm z"),
            new SimpleDateFormat("d MMM yy HH:mm z"),

            new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss"), // without timezone
            new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss"), new SimpleDateFormat("EEE, d MMM yy HH:mm:ss"),
            new SimpleDateFormat("EEE, d MMM yyyy HH:mm"), new SimpleDateFormat("EEE, d MMM yy HH:mm"),
            new SimpleDateFormat("d MMM yyyy HH:mm:ss"), new SimpleDateFormat("d MMM yy HH:mm:ss"),
            new SimpleDateFormat("d MMM yyyy HH:mm"), new SimpleDateFormat("d MMM yy HH:mm"),

            new SimpleDateFormat("EEE, d MMM yy HH:mm:ssz") // newegg.com      Thu, 3 Sep 2009 11:23:10GMT
    };

    public static Date parseRFC822Date(String value) {
        for (SimpleDateFormat formatter : rfc822DateFormats) {
            try {
                Date date = formatter.parse(value);
                return date;
            } catch (ParseException e) {
                continue;
            }
        }

        // progressively trim the space and retry till end of it
        int i = value.lastIndexOf(" ");
        if (i > 6) {
            value = value.substring(0, i);
            return parseRFC822Date(value);
        }
        return null;
    }
}

Related

  1. parseRFC822(String dateString)
  2. parseRFC822(String sDate, Locale locale)
  3. parseRFC822(String str)
  4. parseRfc822Date(String dateString)
  5. parseRfc822Date(String dt)
  6. parseRfc822DateTime(String datestr)