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!
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:
As an example, for the calendar object:
Enter the following:
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); sdf.format(this.getTime());
Ensure Enable this detail formatter is checked
(Source: http://acoderslife.wordpress.com/2009/06/12/eclipse-java-debugging-showing-variable-type-values-calendar/)
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
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());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With