Although we're professionals now, we all started out as humble students - wide-eyed and innocent of the ways of proper coding practices in the corporate world. Back then, everything was new, and we had no real way of knowing whether what we were looking at was wizardry or WTF.
When C. T. was still in school, he ran across a routine for validating dates. It was written in lowly assembler. He found it fascinating, and spent a great deal of time examining its innards. It was designed to run on a 32-bit machine that lacked multiply or divide instructions. It also did something else unusual for the day: it worked on four digit years.
Now nobody expects you to read through a lengthy assembler listing, so he was kind enough to translate it into Java:
private static final int minYear = -99; private static final int maxYear = 25699; /** * Returns true if the inputs constitute a valid date. * @param register2 month in the range: 1 - 12 * @param register3 day in the range: 1 – 31, or as appropriate * @param register4 year in the range: -99 - 25699 * @return true if the date is valid */ public static boolean VALDT4(int register2, int register3, int register4) { // Validate month: 1..12 if (register2 < 1 || register2 > 12) { return false; } // Validate year within the allowed range if (register4 < minYear || register4 > maxYear) { return false; } // If needed, validate the date if (register3 >= 29) { switch (register2) { // April, June, September, November have 30 days case 4: case 6: case 9: case 11: return register3 <= 30; // Gotta love Feb. case 2: { int register5 = (((register4 << 3) + (register4 << 1)) << 2) + register4; register5 -= register5 >> 10; return (register3 <= 29) && ((register4 & 0x3) == 0) && (((register5 & 0xfff) != 0) || ((register5 & 0x3fff) == 0)); } } } // 31-day months return r3 >= 1 && r3 <= 31; }
For those who can't be bothered doing the math, after the register5 calculations are completed, register5 contains: ceiling(register4 * 40.9599609375), which is why the routine breaks down outside the -99..25699 year range limits.