Hi guys,

I am also using java.util.Date in my current project, and I have the same
conversion problems as you. I've just written a
piece-of-code-like-hell-no-design-no-thinking Date converters. Of course,
I'm not using them yet, because the code is horrible, but we could polish it
together. Add LOCALE, error logging, a better structure, and so on.

Here is the horrible piece of code for the converters:

----------------------------------------------------------------------

import java.text.*;
import java.util.*;
import org.apache.commons.beanutils.*;
import org.apache.commons.beanutils.converters.StringConverter;


...

/* you need to register your converters with ConvertUtils in some place,
which is pretty easy */
DateConverter dateConverter = new DateConverter();
StringConverterDateDecorator stringDateConverter = new
StringConverterDateDecorator();
ConvertUtils.register(dateConverter, Date.class);
ConvertUtils.register(stringDateConverter, String.class);

...

class DateConverter implements org.apache.commons.beanutils.Converter
{
        public Object convert(Class type, Object value)
        {
                Object returnValue = null;

                if (value==null)
                        return null;
                else
                if (type==Date.class && value.getClass() == String.class)
                {
                    Date date;
                    try {
                        System.out.println("Date convertor called.");
                        date =
DateFormat.getDateInstance(DateFormat.DEFAULT).parse((String) value);
                        returnValue = date;
                    } catch (ParseException e) {
                        returnValue = null;
                    }
                }
                else
                  returnValue = null;

                return returnValue;
        }
}

class StringConverterDateDecorator implements
org.apache.commons.beanutils.Converter
{
        private static final StringConverter stringConverter = new
StringConverter();

        public Object convert(Class type, Object value)
        {
                Object returnValue = null;

                if (value == null)
                        returnValue = null;
                else
                if (type==String.class &&  value.getClass()==Date.class)
                {
                        System.out.println("String convertor called.");
                        SimpleDateFormat formatter = new 
SimpleDateFormat("dd/MM/yyyy");
                        String s = formatter.format(value);
                        returnValue = s;
                }
                else
                {
                        returnValue = stringConverter.convert(type, value);
                }
                return returnValue;
        }
}

----------------------------------------------------------------------

Regards.
Elder

-----Mensagem original-----
De: James Higginbotham [mailto:[EMAIL PROTECTED]]
Enviada em: quarta-feira, 16 de outubro de 2002 16:36
Para: Rick Reumann
Cc: Struts Users Mailing List
Assunto: RE: Re[4]: Am I the only one using java.util.Date?



    James, I posted myself many times in that thread. That thread does
    not answer the question. It deals with java.sql.Date not
    java.util.Date and yes Craig does mention about custom Converters
    but I'm wondering if others have done that as their solution for
    dealing with java.util.Date and BeanUtils, etc.

My apologies.. I've seen so many dupe posts over the last few weeks.. I
jumped in too quick on this one.

James


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

Reply via email to