Monday, August 16, 2010

Unit tests catch documentation flaws.

I wanted to set a date and clear the time fields for it.

I quickly grabbed a calender object.

Calendar tomorrow = Calendar.getInstance();
tomorrow.add(Calendar.DATE, 1);
tomorrow.clear();

The JavaDoc for .clear() states "Clears the values of all the time fields."  Which is awesome because I want the value to be tomorrow's date with no time.

However, the value of tomorrow at this point is 1/1/1970.  The writer of the documentation considered the date as part of the time fields.   

Instead I have to do this.
Calendar tomorrow = Calendar.getInstance();
tomorrow.add(Calendar.DATE, 1);
tomorrow.set(Calendar.HOUR_OF_DAY, 0);
tomorrow.set(Calendar.MINUTE, 0);
tomorrow.set(Calendar.SECOND, 0);
tomorrow.set(Calendar.MILLISECOND, 0);


This will set the value to tomorrow without any time values.

Documentation is great, but be sure to test the results of the function yourself or you may end up with something other than what you expect.

1 comment:

tenax_technologies said...

Custom software is the most expensive type of development due to it’s nature - unique functionality and utilization of new challenging technologies. The most important thing to understand is that java software development outsourcing is a significant decision that can have lasting ramifications for an organization