validDate Revisited – Java

This entry is part 8 of 13 in the series Zellers Algorithm

With the updated Python function, comes an updated Java function, the code now looks like this, similar to the Python function there are two new checks and a final check that daysInMonth is not still zero:

public boolean validDate(int d, int m, int y)
    {
        int[] monthsThirty0 = {4,6,7,11};
        int[] monthsThirty1 = {1,3,5,7,8,10,12};
        int daysInMonth = 0;

        if (y > 0){
            if ((m > 0) && (m <=12)){
                if (d>0){
                    if (inArray(m, monthsThirty0)){
                        daysInMonth = 30;
                    }else if((inArray(m, monthsThirty1))){
                        daysInMonth = 31;
                    }else if(y%4 == 0){
                        daysInMonth = 29;
                    }else{
                        daysInMonth = 28;
                    }
                }
            }
        }

        if (daysInMonth > 0) {
            return (d <= daysInMonth);
        }else{
            return false;
        }
    }

With this function working correctly, I can now continue with developing the GUI.

Series Navigation<< validDate Revisited – PythonJava GUI >>