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.