Thanks,

I simply was a little confused about the use of the util.DateConverter and webapp.action.DateConverter .

I have used some of yours and followed your advice to use Calendar instead of Timestamp to adjust the webapp.action.Converter and the util.DatuUtils to fit my needs.

It's working alright. I even use the client side TimeZone to convert the Calendar from the usersTimeZone to the system and vis versa. That way times in the system have a correct value for the appropriate timezone. For this I used this http://j2eecookbook.blogspot.com/2007/06/formatting-date-to-client-timezone.html to get users timezone and modified the LocaleFilter.

What I do in the DateUtil to Convert is the following:

from StringToCalendar:
       Calendar calendar = null;
       Date transfer;
       SimpleDateFormat df;
       df = new SimpleDateFormat(aMask);

       if (log.isDebugEnabled()) {
log.debug("converting '" + strDate + "' to date with mask '" + aMask + "'");
       }

       try {
           df.setTimeZone(usersTimeZone);
           transfer = df.parse(strDate);
           calendar = Calendar.getInstance();
calendar.setTime(transfer); } catch (ParseException pe) {
           throw new ParseException(pe.getMessage(), pe.getErrorOffset());
       }
   return calendar;

from CalendarToString:
       SimpleDateFormat df = null;
       String returnValue = "";
       if ( aCalendar == null ) {
           log.error("aCalendar is null!");
       } else {
           df = new SimpleDateFormat( getDateTimePattern() );
           df.setTimeZone( usersTimeZone );
           returnValue = df.format( aCalendar.getTime() );
       }
       return (returnValue);

I would greatly appreciate some thoughts and opinions on this.

kind regards

Dustin Pearce schrieb:
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]



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

Reply via email to