The 37th of March — Java Bug Hunt

Inspired by Java's own java.util.Calendar in lenient mode — the API that happily turns "March 37th" into April 6th.

  • Language: Java
  • Layer: Backend
  • Difficulty: Easy
  • Concepts: Validation, Dates
  • Modelled on: java.util.Calendar
  • Visible tests: valid dates pass through; impossible days are rejected, not rolled; leap day is valid only in leap years
  • Reward: 50 XP for a complete fix

Briefing

Inspired by Java's own java.util.Calendar in lenient mode — the API that happily turns "March 37th" into April 6th. Wonderful for math, catastrophic for validating user input: typos silently become different dates on legal documents.

DateInput.java must reject impossible dates, not "fix" them.

Bug report

BUG-LENIENT · Priority: High (compliance) · Reported by: legal ops

validate(year, month, day) -> int[]{y,m,d} or IllegalArgumentException:

  • month 1..12, day 1..(length of that month, leap-aware)
  • NO rollover. 2024-03-37 is an error, not April 6th.

Observed: contract dates typed with fat fingers silently shift by days.

Logs

[contracts] entered 2024-03-37, stored 2024-04-06

The code as shipped

DateInput.java (editable)

class DateInput {
    static final int[] DAYS = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    static boolean isLeap(int year) {
        return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
    }

    static int[] validate(int year, int month, int day) {
        while (month > 12) {
            month -= 12;
            year++;
        }
        int len = DAYS[month - 1];
        while (day > len) {
            day -= len;
            month++;
            if (month > 12) { month = 1; year++; }
            len = DAYS[month - 1];
        }
        return new int[] { year, month, day };
    }
}

Open the hunt to edit the files, run the visible tests and submit against the hidden ones. More Java bug hunts.