You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Rik Scarborough <ri...@gmail.com> on 2010/08/03 20:03:05 UTC

[betwixt] Writing Calendar

How do you write out a Calendar as a String, such as 1993-12-01T00:00:00Z

I've added a ObjectStringConverter that will convert a Date, but it seems to
ignore the Calendar.

        public String objectToString(Object object, Class type, Context
context) {
            if(object != null) {
                if(object instanceof java.util.Date) {
                    return formatter.format((java.util.Date) object);
                } else if (object instanceof java.util.Calendar) {
                    logger.info("Class type " + type.getName());  //<< never
gets printed.
                    return formatter.format(((java.util.Calendar)
object).getTime());
                }
            }
            return super.objectToString(object, type, context);
        }

I need the same result for Calendar as Date would print out.  Any
suggestions?

~Rik

Re: [betwixt] Writing Calendar

Posted by Rik Scarborough <ri...@gmail.com>.
objectToString is not called directly, but is called by Betwixt at runtime.
 Calendar was not working because the TypeBindingStrategy was returning
Calendar as a complex type not a primitive type. So objectToString was never
called for Calendar.  Overriding the TypeBindingStrategy to return primitive
for java.util.Calendar works; objectToString is called and returns the
proper value.

May be TypeBindingStrategy.Default should be modified to return primitive
for java.util.Calendar.

Thanks,

~Rik

On Tue, Aug 3, 2010 at 5:15 PM, Paul Benedict <pb...@apache.org> wrote:

> To reiterate your requirement, you want a Calendar and Date to print the
> same. So the first thing to do is get your Date instance to print in the
> appropriate format. Then if you pass in a Calendar (as determined by an
> instanceof check), simply call the method again with the Date:
>
> objectToString(((Calendar)object).getTime(), clazz, context);
>
> Paul
>
> On Tue, Aug 3, 2010 at 1:19 PM, Rik Scarborough <ri...@gmail.com> wrote:
>
> > Where do I call getTime() at? I never see the object in the
> > ObjectStringConverter as a Calendar.
> >
> > ~Rik
> >
> > On Tue, Aug 3, 2010 at 1:11 PM, Paul Benedict <pb...@apache.org>
> > wrote:
> >
> > > Call getTime() to transform Calendar to Date, and then use
> > SimpleDateFormat
> > > to transform into a string.
> > >
> > > On Tue, Aug 3, 2010 at 1:03 PM, Rik Scarborough <ri...@gmail.com>
> > wrote:
> > >
> > > > How do you write out a Calendar as a String, such as
> > 1993-12-01T00:00:00Z
> > > >
> > > > I've added a ObjectStringConverter that will convert a Date, but it
> > seems
> > > > to
> > > > ignore the Calendar.
> > > >
> > > >        public String objectToString(Object object, Class type,
> Context
> > > > context) {
> > > >            if(object != null) {
> > > >                if(object instanceof java.util.Date) {
> > > >                    return formatter.format((java.util.Date) object);
> > > >                } else if (object instanceof java.util.Calendar) {
> > > >                    logger.info("Class type " + type.getName());
>  //<<
> > > > never
> > > > gets printed.
> > > >                    return formatter.format(((java.util.Calendar)
> > > > object).getTime());
> > > >                }
> > > >            }
> > > >            return super.objectToString(object, type, context);
> > > >        }
> > > >
> > > > I need the same result for Calendar as Date would print out.  Any
> > > > suggestions?
> > > >
> > > > ~Rik
> > > >
> > >
> >
>

Re: [betwixt] Writing Calendar

Posted by Paul Benedict <pb...@apache.org>.
To reiterate your requirement, you want a Calendar and Date to print the
same. So the first thing to do is get your Date instance to print in the
appropriate format. Then if you pass in a Calendar (as determined by an
instanceof check), simply call the method again with the Date:

objectToString(((Calendar)object).getTime(), clazz, context);

Paul

On Tue, Aug 3, 2010 at 1:19 PM, Rik Scarborough <ri...@gmail.com> wrote:

> Where do I call getTime() at? I never see the object in the
> ObjectStringConverter as a Calendar.
>
> ~Rik
>
> On Tue, Aug 3, 2010 at 1:11 PM, Paul Benedict <pb...@apache.org>
> wrote:
>
> > Call getTime() to transform Calendar to Date, and then use
> SimpleDateFormat
> > to transform into a string.
> >
> > On Tue, Aug 3, 2010 at 1:03 PM, Rik Scarborough <ri...@gmail.com>
> wrote:
> >
> > > How do you write out a Calendar as a String, such as
> 1993-12-01T00:00:00Z
> > >
> > > I've added a ObjectStringConverter that will convert a Date, but it
> seems
> > > to
> > > ignore the Calendar.
> > >
> > >        public String objectToString(Object object, Class type, Context
> > > context) {
> > >            if(object != null) {
> > >                if(object instanceof java.util.Date) {
> > >                    return formatter.format((java.util.Date) object);
> > >                } else if (object instanceof java.util.Calendar) {
> > >                    logger.info("Class type " + type.getName());  //<<
> > > never
> > > gets printed.
> > >                    return formatter.format(((java.util.Calendar)
> > > object).getTime());
> > >                }
> > >            }
> > >            return super.objectToString(object, type, context);
> > >        }
> > >
> > > I need the same result for Calendar as Date would print out.  Any
> > > suggestions?
> > >
> > > ~Rik
> > >
> >
>

Re: [betwixt] Writing Calendar

Posted by Rik Scarborough <ri...@gmail.com>.
Where do I call getTime() at? I never see the object in the
ObjectStringConverter as a Calendar.

~Rik

On Tue, Aug 3, 2010 at 1:11 PM, Paul Benedict <pb...@apache.org> wrote:

> Call getTime() to transform Calendar to Date, and then use SimpleDateFormat
> to transform into a string.
>
> On Tue, Aug 3, 2010 at 1:03 PM, Rik Scarborough <ri...@gmail.com> wrote:
>
> > How do you write out a Calendar as a String, such as 1993-12-01T00:00:00Z
> >
> > I've added a ObjectStringConverter that will convert a Date, but it seems
> > to
> > ignore the Calendar.
> >
> >        public String objectToString(Object object, Class type, Context
> > context) {
> >            if(object != null) {
> >                if(object instanceof java.util.Date) {
> >                    return formatter.format((java.util.Date) object);
> >                } else if (object instanceof java.util.Calendar) {
> >                    logger.info("Class type " + type.getName());  //<<
> > never
> > gets printed.
> >                    return formatter.format(((java.util.Calendar)
> > object).getTime());
> >                }
> >            }
> >            return super.objectToString(object, type, context);
> >        }
> >
> > I need the same result for Calendar as Date would print out.  Any
> > suggestions?
> >
> > ~Rik
> >
>

Re: [betwixt] Writing Calendar

Posted by Paul Benedict <pb...@apache.org>.
Call getTime() to transform Calendar to Date, and then use SimpleDateFormat
to transform into a string.

On Tue, Aug 3, 2010 at 1:03 PM, Rik Scarborough <ri...@gmail.com> wrote:

> How do you write out a Calendar as a String, such as 1993-12-01T00:00:00Z
>
> I've added a ObjectStringConverter that will convert a Date, but it seems
> to
> ignore the Calendar.
>
>        public String objectToString(Object object, Class type, Context
> context) {
>            if(object != null) {
>                if(object instanceof java.util.Date) {
>                    return formatter.format((java.util.Date) object);
>                } else if (object instanceof java.util.Calendar) {
>                    logger.info("Class type " + type.getName());  //<<
> never
> gets printed.
>                    return formatter.format(((java.util.Calendar)
> object).getTime());
>                }
>            }
>            return super.objectToString(object, type, context);
>        }
>
> I need the same result for Calendar as Date would print out.  Any
> suggestions?
>
> ~Rik
>