Tuesday, March 31, 2009

Romans, Calendars, Java, and Bad Bugs

Well, I learned today that Java is not smart enough to figure out that if there aren't 31 days in a month, just to go to the last day. Now I may be the last self-respecting Java developer on the planet to figure this out, but if not, here's a tip:

1) use the roll() method to push an element of the date forward, without affecting the other days.
2) Don't instantiate your GregorianCalendar and then just add a month, because if you move to April 31, it automatically becomes May 1. Instead, you can also just create a new Calendar with the day set to 1, and then new month set correctly.

Java, you vex me so. What a terrible bug by me. And without the benefits of unit tests, not something I ever would have found out until we hit a 31st day of a month, or until we reached January and went into February with it's 28 days. Sigh.
blog comments powered by Disqus