javaleap-year

Determine leap year using Java?


Here is my requirements. Determine If a Year Is a Leap Year Algorithm:

I need to know if i did right?

private static boolean isLeapYear(int userInput){
    boolean leapYear= false;

    if (userInput % 4 == 0 ){
        leapYear = true;

        if (userInput % 4 == 0 && userInput % 100 ==0) {
            leapYear = false;

            if(userInput % 400 == 0){
                leapYear = true;
            }
        }
    }
    else {
        leapYear = false;
    }

    return leapYear;
}

Solution

  • userInput % 4 == 0 && userInput % 100 ==0 is equivalent to userInput % 400 == 0

    and userInput % 4 == 0 then it is definitely Leap year so need not to check any other condition.