I use a slightly modified DateConverter for Dates and a CalendarConverter for DateTimes. I modified it so I could accept multiple input formats but always print out the same format.

public class DateConverter extends DefaultTypeConverter {
    Log log = LogFactory.getLog(DateConverter.class);

    public Object convertValue(Map map, Object object, Class aClass) {
/ ***********************************************************Set Standard Format*/ String[] parsePatterns = {"MM/dd/yyyy", "dd-MMM-yyyy", "MM.dd.yyyy", "mm/dd/yy"}; FastDateFormat df = FastDateFormat.getInstance(parsePatterns[1]);
        if (aClass == Date.class) {
/ ********************************************************Get First Value in Parameters Array*/
            String source = ((String[]) object)[0];
            Date transfer;
            try {
                transfer = DateUtils.parseDate(source, parsePatterns);
                return transfer;
            } catch (ParseException e) {
throw new RuntimeException("Cannot convert " + source + " to calendar type");
            }
        } else if (aClass == String.class) {
            Date o = (Date) object;
            return df.format(o);
        }
        return null;
    }
}



public class CalendarConverter extends DefaultTypeConverter {
    Log log = LogFactory.getLog(CalendarConverter.class);

    public Object convertValue(Map map, Object object, Class aClass) {
/ ***********************************************************Set Standard Format*/
        String[] parsePatterns = {
                "MM/dd/yyyy hh:mm a",
                "MM/dd/yyyy hh:mm:ss a",
                "dd-MMM-yyyy hh:mm a",
                "dd-MMM-yyyy hh:mm:ss a",
                "MM/dd/yyyy HH:mm",
                "MM/dd/yyyy HH:mm:ss",
                "dd-MMM-yyyy HH:mm",
                "dd-MMM-yyyy HH:mm:ss",
        };
FastDateFormat df = FastDateFormat.getInstance(parsePatterns[0]);
        if (aClass == Calendar.class) {
/ ********************************************************Get First Value in Parameters Array*/
            String source = ((String[]) object)[0];
/ ********************************************************Create Target Calendar Object******/
            Calendar returnCal = new GregorianCalendar();
            Date transfer;
            try {
/ ********************************************************Call Commons DateUtils parse with array of patterns*/ / ********************************************************Currently only one pattern that forces the time to be*/ / ********************************************************present. Could include a MM/dd/yyyy pattern but you*/ / ********************************************************should use a java.util.Date object for that type*/
                transfer = DateUtils.parseDate(source, parsePatterns);
                returnCal = new GregorianCalendar();
                returnCal.setTime(transfer);
                return returnCal;
            } catch (ParseException e) {
throw new RuntimeException("Cannot convert " + source + " to calendar type");
            }
        } else if (aClass == String.class) {
            Calendar o = (Calendar) object;
            log.debug(o.getTime());
            return df.format(o.getTime());
        }
        return null;
    }
}





On Aug 19, 2008, at 9:51 AM, Matt Raible wrote:

You might try creating a new converter that specifically supports Timestamp.

Matt

On Sun, Aug 17, 2008 at 9:26 AM, Kropp, Henning <[EMAIL PROTECTED]> wrote:
Hi,

I am trying to figure out how to use the DateConverter in Appfuse 2.0.2 to
convert Timestamp to String and vis versa.

At first I used java.util.Date and the DateConverter worked fine, than I
changed the type in the POJOs to java.sql.Timestamp. To have the
java.sql.Timestamp be converted I also added
java.sql.Timestamp=com.app.util.DateConverter to the
xwork-conversion.properties.

But now running under mvn jetty:run (not quite sure what version - using 6.1.9 maven-jetty-plugin) the DateConverter does not "kick-in" for the
java.sql.Timestamp values. For Date still works great.

What am I missing?

Thanks and kind regards


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to