public static String bestBefore(String input) {

        int[] numbers = splitDate(input);

        BasicDate earliest = null;

        for (int i=0, d, m, y; i<3; i++) {

            d = numbers[i];       // day
            m = numbers[(i+1)%3]; // month (or year, if it cannot be a month)
            y = numbers[(i+2)%3]; // year (or month, if it cannot be a year)

            // we need to store each possibility to check later
            // which date is the earliest one

            if (canBeADay(d, m, y)) {
                if (earliest == null) {
                    earliest = new BasicDate(d, m, y);
                }
                else if (!earliest.isEarlierThan(d, m, y)) {
                    earliest.update(d, m, y);
                }
            }

            if (canBeADay(d, y, m)) {
                if (earliest == null) {
                    earliest = new BasicDate(d, y, m);
                }
                else if (!earliest.isEarlierThan(d, y, m)) {
                    earliest.update(d, y, m);
                }
            }
        }

        return (earliest == null) ? input+" is illegal" : earliest.toString();
    }