commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Voyer Henry <Henry.Vo...@loto-quebec.com>
Subject Betwixt Help : Marshalling process for custom java.util.Date
Date Thu, 23 Oct 2003 16:08:59 GMT
Hi everyone.

After reading and testing betwixt for marshaling dates, im still very
confused.

As an example I have implemented a converter for my date based on
java.sql.Date for java.util.Date
See code 1.

Before calling the marshalling and unmarshalling process I register my date
converter

   ConvertUtils.register(new DateConverter(),java.util.Date.class);

When marshalling it never calls the converter but when it calls it it get
the error since it took Date.toString() to
Marshall the content......

Can someone tell me what im doing wrong ? And whats the best way to
customize the format of my date so it can read it and write it ?

Thanks and regards
Henry


Atch 1
Etc ...
    private Object defaultValue = null;
    private boolean useDefault = true;
    final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");


    public DateConverter() {
        this.defaultValue = null;
        this.useDefault = false;
    }


    public DateConverter(Object defaultValue) {
        this.defaultValue = defaultValue;
        this.useDefault = true;
    }

    
    public Object convert(Class type, Object value) {

        if (value == null) {
            if (useDefault) {
                return (defaultValue);
            } else {
                throw new ConversionException("No value specified");
            }
        }

        if (value instanceof Date) {
            return (formatter.format((Date)value));
        }

        try {
            return (formatter.parse(value.toString()));
        } catch (Exception e) {
            if (useDefault) {
                return (defaultValue);
            } else {
                throw new ConversionException(e);
            }
        }
    }
}

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message