Here is a Calendar converter class.  Modify as necessary for your calendar
object:

import ognl.DefaultTypeConverter;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

/**
 */
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",
                "MM/dd/yyyy",
                "dd-MMM-yyyy"
        };
        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;
    }
}


bilobag wrote:
> 
> I have a collection of XMLGregorianCalendar objects that I would like to
> display on a jsp page as a formatted date.  So far I have tried creating
> my own converter class to convert XMLGregorianCalendar to java.util.Date,
> but I can't seem to get it called properly.  What is the best way to get
> this list to display on a jsp page as formatted dates?  I didn't see much
> documentation on how to create a converter to convert from one object type
> to another.  Another option is for me to be able to call the
> XMLGregorianCalendar methods on the jsp page like below, but that didn't
> seem to work either.  Anybody know the answer?
> 
> 
> <s:iterator value="scheduleList" status="scheduleStatus">
>       <s:date value="scheduleList.toGregorianCalendar().getTime()"
> format="dd/MM/yyyy"/>
> </s:iterator>
> 

-- 
View this message in context: 
http://www.nabble.com/Display-XMLGregorianCalendar-as-date-on-jsp-page-using-type-converter-tp21821248p21824010.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to