Now blogging at diego's weblog. See you over there!

outputting dates in RFC822 and ISO8601 formats

Okay, this is something else that is simple but generally requires to look at specs just to see how to do it properly. And these days, what with everyone generating RSS and such, creating properly formatted RFC822 and ISO 8601 dates is important to make sure the feeds validate. So here's the code, using Java's SimpleDateFormat, to output both formats properly.


import java.util.Date;
import java.text.SimpleDateFormat;

//... class definition here...

public static SimpleDateFormat ISO8601FORMAT
    = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
public static SimpleDateFormat RFC822DATEFORMAT
    = new SimpleDateFormat("EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z", Locale.US);

public static String getDateAsRFC822String(Date date)
{
  return RFC822DATEFORMAT.format(date);
}

public static String getDateAsISO8601String(Date date)
{
  String result = ISO8601FORMAT.format(date);
  //convert YYYYMMDDTHH:mm:ss+HH00 into YYYYMMDDTHH:mm:ss+HH:00
  //- note the added colon for the Timezone
  result = result.substring(0, result.length()-2)
    + ":" + result.substring(result.length()-2);
  return result;
}


Later: Aside from some fixes due to comments (thanks!), Erik, via IM, notes that RFC 822 has been superseded by RFC 2822, in which years on dates use four digits rather than two. The RSS Spec actually encourages the use of four digits from RFC 2822 (even though it explicitly mentions RFC 822 only). So I modified the code for that.

Also, Zoe, via email, notes that a cleaner way of generating ISO 8601 dates is to use the UTC designator "Z", which has the added advantage of making dates easier to parse back in Java. Clearly this is a better solucion when possible, however, this means that all times have to be changed into UTC, something that might not be desirable in many cases where you want to maintain Timezone information. The parsing of an ISO 8601 date with the colon is similarly "dirty" by having to remove the ":" by hand before being able to parse it from Java.

Categories: soft.dev
Posted by diego on January 4, 2005 at 2:26 PM

hiding mailto: addresses with javascript

Making it harder for crawlers to discover email addresses is something that is not hard to do, but it's one of those things that I at least keep redoing just because it's so easy to do. But it should be instantaneous! :) So here's some code, which I've written in different forms over the years, neatly packaged.

What the code does is run a simple substitution cypher on the address, then shows all the code you'd need to include on a page to make it work (using simple CSS/DHTML tricks, making a layer visible). It also lets you include text to put on the links, so that a single Javascript call put anywhere on the page generates the full mailto: link.

By changing the ordering of letters in the key variable, you can change the way it encrypts things for your page. Okay, "encryption" maybe a bit of an overstatement here, but technically... :)

Just for clarity, here's the link again. It's all under an MIT License, so you're free to do with it as you will. Hope this is useful!

Categories: soft.dev
Posted by diego on January 4, 2005 at 12:57 PM

Copyright © Diego Doval 2002-2011.