Thursday, December 09, 2004

Formatting and sorting dates in Displaytag

I use Displaytag for displaying data in an as400 Work-With Subfile type format in web pages, and it works very well.

One problem I came across was formatting the dates, whilst still using the automatic sorting. I thought I would have to format my data in my POJO and lose the automatic sorting.

But in fact Displaytag solves this problem by allowing you to apply decorators to columns

eg:

package com.decorator;

import java.util.Date;

import org.apache.commons.lang.time.FastDateFormat;
import org.displaytag.decorator.ColumnDecorator;


/**
* Simple column decorator which formats a date.
* @author epesh
* @author Fabrizio Giustina
* @version $Revision: 1.11 $ ($Author: fgiust $)
*/
public class DatetoDMY implements ColumnDecorator
{

/**
* FastDateFormat used to format the date object.
*/
private FastDateFormat dateFormat = FastDateFormat.getInstance("dd.MM.yyyy HH:mm:ss"); //$NON-NLS-1$

/**
* transform the given object into a String representation. The object is supposed to be a date.
* @param columnValue Object
* @return String
*/
public final String decorate(Object columnValue)
{
Date date = (Date) columnValue;
return this.dateFormat.format(date);
}
}

2 comments:

  1. Anonymous10:44 am

    uhh...hi
    how to use this ?

    ReplyDelete
  2. Anonymous10:45 am

    how to use what, displaytag or the decorator?

    ReplyDelete