Java Parse RFC Date parseRFC822(String sDate, Locale locale)

Here you can find the source of parseRFC822(String sDate, Locale locale)

Description

parse RFC

License

Apache License

Declaration

public static Date parseRFC822(String sDate, Locale locale) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    private static final String[] RFC822_MASKS = { "EEE, dd MMM yyyy HH:mm:ss z", "EEE, dd MMM yyyy HH:mm:ss Z",
            "EEE, dd MMM yy HH:mm:ss z", "EEE, dd MMM yy HH:mm z", "dd MMM yy HH:mm:ss z", "dd MMM yy HH:mm z" };

    public static Date parseRFC822(String sDate, Locale locale) {
        int utIndex = sDate.indexOf(" UT");
        if (utIndex > -1) {
            String pre = sDate.substring(0, utIndex);
            String post = sDate.substring(utIndex + 3);
            sDate = pre + " GMT" + post;
        }//  w  ww  . j a  v a 2s .com
        return parseUsingMask(RFC822_MASKS, sDate, locale);
    }

    private static Date parseUsingMask(String[] masks, String sDate, Locale locale) {
        if (sDate != null) {
            sDate = sDate.trim();
        }
        ParsePosition pp = null;
        Date d = null;
        for (int i = 0; (d == null) && (i < masks.length); i++) {
            DateFormat df = new SimpleDateFormat(masks[i], locale);

            df.setLenient(true);
            try {
                pp = new ParsePosition(0);
                d = df.parse(sDate, pp);
                if (pp.getIndex() != sDate.length()) {
                    d = null;
                }
            } catch (Exception localException) {
            }
        }
        return d;
    }
}

Related

  1. parseRfc3339Calendar(String value)
  2. parseRfc3339Date(String date)
  3. parseRFC3339Date(String datestring)
  4. parseRFC3339Date(String datestring)
  5. parseRFC822(String dateString)
  6. parseRFC822(String str)
  7. parseRfc822Date(String dateString)
  8. parseRfc822Date(String dt)
  9. parseRFC822Date(String value)