You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Rick Reumann <ma...@reumann.net> on 2002/10/17 18:22:35 UTC

[ConvertUtils] Converter for java.util.Date

Hey Elder,

(posting this to Struts list as well in case anyone else finds it
useful while digging through the archives)

I modified the code you sent just a bit and also provided a method
taking a format String for how they want the date formatted (actually
someone might want to modify the code with a default format, but I
want to be forced to provide one). I still haven't gotten around to
actually modifying the StringConverter class to handle the other
direction (String to java.util.Date) to avoid having to register the
extra custom StringConverter class below. I'll get around to it some
time:)

To use these converters so that BeanUtils.copyProperties( ) works in
both directions I just added a static block to the top of my dispatch
Action ...

static {
  DateBeanUtilsConverter dateConverter = new DateBeanUtilsConverter();
  dateConverter.setFormatPattern( "MMddyyyy" );
  StringBeanUtilsConverterDate myStringConverter = new StringBeanUtilsConverterDate();
  myStringConverter.setFormatPattern( "MMddyyyy" );
  ConvertUtils.register( dateConverter, java.util.Date.class );
  ConvertUtils.register( myStringConverter, String.class );
}

The two classes are listed below. All seems to be working fine.

/** coverts java.util.Date to String using BeanUtils ***/

import org.apache.commons.beanutils.Converter;
import java.text.*;
import java.util.*;
import corporate.*;

public class DateBeanUtilsConverter implements Converter {

    private String formatPattern = null;

    public void setFormatPattern(String formatPattern) {
        this.formatPattern = formatPattern;
    }

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

        if (value != null
            && (value instanceof String)
            && (type == Date.class)) {
            try {

                String s = value.toString();
                SimpleDateFormat formatter =
                    new SimpleDateFormat(formatPattern);
                date = formatter.parse(s);

            } catch (Exception e) {
                ErrorLogging.println("DateBeanUtilsConverter: " + e);
            }
        }
        return date;
    }
}

/** coverts String to java.util.Date using BeanUtils ***/

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

public class StringBeanUtilsConverterDate implements Converter {
    private static final StringConverter stringConverter =
        new StringConverter();
    private String formatPattern = null;

    public void setFormatPattern(String formatPattern) {
        this.formatPattern = formatPattern;
    }

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

        if (value != null) {
            if (type == String.class && (value instanceof Date)) {
                SimpleDateFormat formatter =
                    new SimpleDateFormat(formatPattern);
                String dateString = formatter.format(value);
                returnValue = dateString;
            } else {
                returnValue = stringConverter.convert(type, value);
            }
        }
        return returnValue;
    }
}

-- 

Rick
mailto:maillist@reumann.net


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RES: [ConvertUtils] Converter for java.util.Date

Posted by Elderclei R Reami <re...@vertisnet.com.br>.
Hey Rick,

I'm back to work now, after some 14h sleeping :) I saw your changes, and the
code is now better. I'm justing adding it to my build.

Maybe Craig could help us add this to contrib, or something, hah?! :)

Cheers,
Elder

-----Mensagem original-----
De: Rick Reumann [mailto:maillist@reumann.net]
Enviada em: quinta-feira, 17 de outubro de 2002 13:23
Para: Elderclei R Reami
Cc: Struts List
Assunto: [ConvertUtils] Converter for java.util.Date


Hey Elder,

(posting this to Struts list as well in case anyone else finds it
useful while digging through the archives)

I modified the code you sent just a bit and also provided a method
taking a format String for how they want the date formatted (actually
someone might want to modify the code with a default format, but I
want to be forced to provide one). I still haven't gotten around to
actually modifying the StringConverter class to handle the other
direction (String to java.util.Date) to avoid having to register the
extra custom StringConverter class below. I'll get around to it some
time:)

To use these converters so that BeanUtils.copyProperties( ) works in
both directions I just added a static block to the top of my dispatch
Action ...

static {
  DateBeanUtilsConverter dateConverter = new DateBeanUtilsConverter();
  dateConverter.setFormatPattern( "MMddyyyy" );
  StringBeanUtilsConverterDate myStringConverter = new
StringBeanUtilsConverterDate();
  myStringConverter.setFormatPattern( "MMddyyyy" );
  ConvertUtils.register( dateConverter, java.util.Date.class );
  ConvertUtils.register( myStringConverter, String.class );
}

The two classes are listed below. All seems to be working fine.

/** coverts java.util.Date to String using BeanUtils ***/

import org.apache.commons.beanutils.Converter;
import java.text.*;
import java.util.*;
import corporate.*;

public class DateBeanUtilsConverter implements Converter {

    private String formatPattern = null;

    public void setFormatPattern(String formatPattern) {
        this.formatPattern = formatPattern;
    }

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

        if (value != null
            && (value instanceof String)
            && (type == Date.class)) {
            try {

                String s = value.toString();
                SimpleDateFormat formatter =
                    new SimpleDateFormat(formatPattern);
                date = formatter.parse(s);

            } catch (Exception e) {
                ErrorLogging.println("DateBeanUtilsConverter: " + e);
            }
        }
        return date;
    }
}

/** coverts String to java.util.Date using BeanUtils ***/

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

public class StringBeanUtilsConverterDate implements Converter {
    private static final StringConverter stringConverter =
        new StringConverter();
    private String formatPattern = null;

    public void setFormatPattern(String formatPattern) {
        this.formatPattern = formatPattern;
    }

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

        if (value != null) {
            if (type == String.class && (value instanceof Date)) {
                SimpleDateFormat formatter =
                    new SimpleDateFormat(formatPattern);
                String dateString = formatter.format(value);
                returnValue = dateString;
            } else {
                returnValue = stringConverter.convert(type, value);
            }
        }
        return returnValue;
    }
}

--

Rick
mailto:maillist@reumann.net



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>