You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by bu...@apache.org on 2003/07/16 20:48:17 UTC

DO NOT REPLY [Bug 21663] New: - [lang] Add support in ToStringStyle for DateFormat.

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=21663>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=21663

[lang] Add support in ToStringStyle for DateFormat.

           Summary: [lang] Add support in ToStringStyle for DateFormat.
           Product: Commons
           Version: 1.0.1 Final
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: Lang
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: ggregory@seagullsw.com


This stems from a discussion on commons-user.

The basic idea is that ToStringStyle could be customized to include an optional
java.text DataFormat object. If the attched patch were in (mod some formatting
and unit tests which I can do), user could write code like this:

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class TestRTSB {

    static class MyToStringStyle extends ToStringStyle {
        MyToStringStyle() {
            this.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        }
    }

    public static void main(String[] arguments) {
        System.out.println(new TestRTSB());
    }
    
    private int foo = 22;

    private Date myDate = new Date();

    public String toString() {
        return new ReflectionToStringBuilder(this, new
MyToStringStyle()).toString();
    }

// Use ToStringStyle.setDateFormat instead of:
//
//    public String toString0() {
//        return (new ReflectionToStringBuilder(this) {
//            protected Object getValue(Field field) throws
IllegalArgumentException, IllegalAccessException {
//                Object value = super.getValue(field);
//                if ("myDate".equals(field.getName())) {
//                    return new SimpleDateFormat("yyyy-MM-dd").format(value);
//                } else {
//                    return value;
//                }
//            }
//        }).toString();
//    }
}

Any thoughts?

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org