Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can the Eclipse toString code generator be changed to display less verbose Calendar objects

Tags:

java

eclipse

The Eclipse toString source generator is extremely helpful except when objects contain a Calendar object. Is there a way to change the Eclipse toString generator to display Calendar objects more cleanly? Possibly using a common SimpleDateFormat? Or a trustworthy plugin?

Desired Calendar toString ("yyyy-MM-dd HH:mm:ss"):

2011-08-22 13:29:26

Sample Eclipse Calendar toString (this is pretty ridiculous):

java.util.GregorianCalendar[time=1314034166984,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2011,MONTH=7,WEEK_OF_YEAR=35,WEEK_OF_MONTH=4,DAY_OF_MONTH=22,DAY_OF_YEAR=234,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=29,SECOND=26,MILLISECOND=984,ZONE_OFFSET=-18000000,DST_OFFSET=3600000]

FuryComputers led me to a solution. Here is a quick and dirty Custom toString implementation:

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MyToStringBuilder {
    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private String str;
    private boolean isFirst;

    public MyToStringBuilder(Object o) {
        isFirst = true;
        str = "";
        str += o.getClass().getSimpleName() + " [";
    }

    public MyToStringBuilder appendItem(String variableName, Object value) {
        if(value != null){
            this.addItem(variableName, value.toString());
        } else {
            this.addItem(variableName, "null");
        }
        return this;
    }

    public MyToStringBuilder appendItem(String variableName, Calendar value) {
        if(value != null){
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
            this.addItem(variableName, sdf.format(value.getTime()));
        } else {
            this.addItem(variableName, "null");
        }
        return this;
    }

    private void addItem(String variableName, String value) {
        if(!isFirst){
            str += ", ";
        }
        isFirst = false;
        str += variableName + "=";
        str += value;
    }

    @Override
    public String toString() {
        str += "]";
        return str;
    }

}

Thanks!

like image 779
HeavyE Avatar asked Aug 22 '11 17:08

HeavyE


3 Answers

The following effectively overwrites the toString() method for any class as seen using the Eclipse Debugger only, but I've found it very useful in decoding calendar objects while I debug.

Add a Debug Detail Formatter, using these steps:

  1. From the menu select Windows->Preferences
  2. In Preferences open Java->Debug and click on Detail Formatters
  3. Here you can add your desired formatter

As an example, for the calendar object:

  1. Click Add..
  2. Enter java.util.Calendar into the Qualified type name field
  3. Enter the following:

    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); sdf.format(this.getTime());

  4. Ensure Enable this detail formatter is checked

  5. Click OK.

(Source: http://acoderslife.wordpress.com/2009/06/12/eclipse-java-debugging-showing-variable-type-values-calendar/)

like image 183
gdw2 Avatar answered Nov 04 '22 21:11

gdw2


When you generate a toString method in eclipse, the dialog has a "Code Style" option. In that list is "Custom toString() Builder" which you can define your own String Builder type class to use.

It is (poorly) documented here under "Custom toString() builder" http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.user/reference/ref-tostring-styles.htm

like image 44
FuryComputers Avatar answered Nov 04 '22 20:11

FuryComputers


Eclipse just uses the GregorianCalendar.toString() method. The only way to change this would be to extend the GregorianCalendar class in your own Object and implement a custom toString() method.

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

public class MyGregorianCalendar extends GregorianCalendar
{
    public MyGregorianCalendar() {
        super();
    }

    SimpleDateFormat sdf = new SimpleDateFormat("H:MM:d kk:m:s");
    public String toString() {
        return sdf.format(getTime());
    }
}
like image 34
JJ. Avatar answered Nov 04 '22 22:11

JJ.