Re: Display XMLGregorianCalendar as date on jsp page using type converter

2009-02-04 Thread bilobag

Thanks for the class.  I have a few questions about it.  What exactly is the
point of the array of parse patterns?  It seems like you only use the first
one.  It would be great if there was a way for me to specify a parse pattern
as the conversion is happening because in different situations I want a
different date format.  The previous person was also correct.   I want to
call the date format tag on the specific list object rather than on the
whole list.  However so far I can only get my converter to be automatically
called if I try to display the address using the  tag because it
calls toString.  I ideally would like my converter to convert an
XMLGregorianCalendar object to a date object and then I can use the 
tag on that date object.  I can't seem  to get my converter to be
automatically called in this situation.  Any thoughts on this?

This is the code I would ideally use and have the converter automatically
convert my XMLGregorianCalendar object to a date object and use the struts
tag to format my date:


  




dusty wrote:
> 
> 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/ hh:mm a",
> "MM/dd/ hh:mm:ss a",
> "dd-MMM- hh:mm a",
> "dd-MMM- hh:mm:ss a",
> "MM/dd/ HH:mm",
> "MM/dd/ HH:mm:ss",
> "dd-MMM- HH:mm",
> "dd-MMM- HH:mm:ss",
> "MM/dd/",
> "dd-MMM-"
> };
> 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/ 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.  A

Display XMLGregorianCalendar as date on jsp page using type converter

2009-02-03 Thread bilobag

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?



  

-- 
View this message in context: 
http://www.nabble.com/Display-XMLGregorianCalendar-as-date-on-jsp-page-using-type-converter-tp21821248p21821248.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