RFC 822 Date parsing in Java

| | Comments (3) | TrackBacks (0)
I was working on a RSS to JSON XSL transform for some work related stuff. For reasons not to be disclosed, the updated timestamp of the item was to be parsed and serialized into JSON as an array of integers. There were bunch of problems to that. XSL or EXSL, for that matter, does not have support for parsing RFC822 dates. This can be solved by writing my own template for parse the date or even better write a static java method and use that in the transformation. I took the later approach (coz its easier) because its easier and having a our code base could use a utility method to parse RFC822 dates. Even better I wrote it so that it will take in a RFC822 date string and return a Atom date (ISO whatever same as XSD date). So now my transformation can parse the date and give me the individual bits i needed and also our codebase has a nice utility method(this such a lame reason...). Parsing a RFC822 date (for that any matter any standard format date) is a bit tricky thing in java. Well, its not all that tricky now that I have figured it. We have the DateFormat class we can use to parse the date string to date. But as RFC822 date though a single format has there are parts of the dates which are optional e.g. second, Day of the Week. so we need multiple DateFormat instances (actually SimpleDateFormat) to support the format fully ( I realized this only after my transform started failing for feed which were not providing "seconds" values in the timestamp). Below is the code snippet to parse the date string.
public static final SimpleDateFormat rfc822DateFormats[] = new SimpleDateFormat[] { new SimpleDateFormat("EEE, d MMM yy HH:mm:ss z"), new SimpleDateFormat("EEE, d MMM yy HH:mm z"), new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z"), new SimpleDateFormat("EEE, d MMM yyyy HH:mm z"), new SimpleDateFormat("d MMM yy HH:mm z"), new SimpleDateFormat("d MMM yy HH:mm:ss z"), new SimpleDateFormat("d MMM yyyy HH:mm z"), new SimpleDateFormat("d MMM yyyy HH:mm:ss z"), };
I want to note that the order in which this objects are applied is important. Mainly because of the effect of using "yy" and "yyyy". You can lookup the javadocs for the pattern to see what I am saying. Closing note: I like the fact that Atom has adopted the ISO date standard, I think its more widely supported.

Categories

0 TrackBacks

Listed below are links to blogs that reference this entry: RFC 822 Date parsing in Java.

TrackBack URL for this entry: http://jimyjoshi.com/cgi-bin/mt/mt-tb.cgi/15

3 Comments

neurk said:

Hi!
The format seems correct to me but does not work if I try to parse the date from an RSS fedd. The date is "Sun, 17 Dec 2006 00:00:00 +0100" and seems to match the pattern but I get
java.text.ParseException: Unparseable date: "Sun, 17 Dec 2006 00:00:00 +0100".

Java version is 1.5.
Are there any known problems with the DateFormat?

Greetings,
neurk

neurk said:

Hi!
I want to add, that it is important to use the right locale because to names for month and days are depend on it. The Locale.US seems to work fine.

Greeting.

Jaimini Author Profile Page said:

@neurk,

Sorry for the delay. I tested the format the the date you gave. it parses well for me. I have tested in on jdk 1.5 and 1.6 as well. the formats which pass the parsing are "EEE, d MMM yy HH:mm:ss z" and "EEE, d MMM yyyy HH:mm:ss z".

Leave a comment

About this Entry

This page contains a single entry by Jaimini published on August 3, 2007 6:13 PM.

Orkut and VTech shootings was the previous entry in this blog.

India Podcast is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.