« - »

ISO 8601 Date Formatting

23 February 2008

There are a number of little clean-up things that need to be done before we can consider the Approval Service to really be complete, and putting all of the dates in the XML into some kind of standard format is one of the items on that list. I decided to go with ISO 8601 because that seems to be the current de facto standard.

Since we are using Betwixt to generate the XML, which uses Commons BeanUtils under the hood, the appropriate way to convert the format of anything is to build and register a converter for that purpose. I figured that there must be a simple ISO 8601 date converter already built by someone, but my Google skills must not be what they used to be, because I couldn’t find one. I ended up building one, and sticking it into the restafarian.jar file:

package org.restafarian.core.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;

 * <p>Converts java Date objects to ISO8601-compliant String
 * values and back.</p>
public class ISO8601DateConverter implements Converter {
  private static final DateFormat FORMAT = 
       new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

  public Object convert(Class clazz, Object object) throws
         ConversionException {
    Object returnValue = null;

    if (object != null) {
      if (clazz == null) {
        throw new ConversionException("Class parameter cannot
             be null.");
      } else {
        if (clazz.equals(Date.class) &&
               object.getClass().equals(String.class)) {
          returnValue = convertStringToDate((String) object);
        } else if (clazz.equals(String.class)) {
          if (object.getClass().equals(Date.class) ||
                 object.getClass().equals(Timestamp.class)) {
            returnValue = convertDateToString((Date) object);
          } else {
            returnValue = object.toString();
        } else {
          returnValue = object;

    return returnValue;

  public Date convertStringToDate(String string) throws
         ConversionException {
    try {
      return FORMAT.parse(string);
    } catch (ParseException e) {
      throw new ConversionException(e);

  public String convertDateToString(Date date) throws
         ConversionException {
    String string = FORMAT.format(date);
    return string.substring(0, string.length() - 2) + ":" +
         string.substring(string.length() - 2);

Once that was done, all that was left was to modify the Betwixt tool to register the converter before generating the XML:

// set up ISO 8601 date convertion
Converter dateConverter = new ISO8601DateConverter();
ConvertUtils.register(dateConverter, Date.class);
ConvertUtils.register(dateConverter, Timestamp.class);
ConvertUtils.register(dateConverter, String.class);

That takes care of the POJO-to-XML problem. But now that we have ISO 8601 dates embedded in the XML, we’re going to have to do something on the presentation side of things to pretty them up. Surely, that work has already been done by someone else, and we can just leverage off of their labors without having to actually build something. Time to sharpen up those Google skills …


Comments are closed.

Sorry, the comment form is closed at this time.