Sunday, July 26, 2009

Using Restlet to create a RFC 1123 formatted Date

I have a service that uses Restlet 1.1.1 where the client requested a header be returned in RFC 1123 format. I needed to override the default returned by the container in use for the header to match the standard format.

In order to accommodate the request, I looked into the Restlet API and found the DateUtils class.

This little example hopefully will give someone with a similar need something to go on:

import java.util.List;
import java.util.Date;
import org.restlet.util.DateUtils;

class Rfc {
public static void main(String[] args) {
List format = DateUtils.FORMAT_RFC_1123;
String formatString = format.get(0);
Date date = new Date();
String formattedDate = DateUtils.format(date, formatString);

System.out.println("Format: " + formatString);
System.out.println("Original Date: " + date);
System.out.println("Formatted Date: " + formattedDate);
}
}
The output from a run of this class:
Format: EEE, dd MMM yyyy HH:mm:ss zzz
Original Date: Sun Jul 26 11:26:45 EDT 2009
Formatted Date: Sun, 26 Jul 2009 15:26:45 GMT

2 comments:

  1. But why wouldn't Restlet format HTTP header dates as RFC 1123 by default?

    ReplyDelete
  2. I would imagine Restlet would. Currently, the Mule Restlet transport does not map all outgoing response headers, so in this case I have to add it manually.

    ReplyDelete