Rectangle 27 0

Apart from the algorithm you can calculate leap year using java's built in Calendar api.

static boolean isLeapYear(int year){
    Calendar calendar= Calendar.getInstance();
    calendar.set(Calendar.YEAR,year);
    return calendar.getActualMaximum(Calendar.DAY_OF_YEAR) > 365;
}

jgrasp - Java leap year code problems - Stack Overflow

java jgrasp leap-year
Rectangle 27 0

Apart from the algorithm you can calculate leap year using java's built in Calendar api.

static boolean isLeapYear(int year){
    Calendar calendar= Calendar.getInstance();
    calendar.set(Calendar.YEAR,year);
    return calendar.getActualMaximum(Calendar.DAY_OF_YEAR) > 365;
}

jgrasp - Java leap year code problems - Stack Overflow

java jgrasp leap-year
Rectangle 27 0

Firstly, this if((year < 1582) == (year % 4==0)) checks boolean equality. I think you wanted an if((year < 1582) && (year % 4==0)) but I'm afraid that still doesn't fix your logic.

I suggest you start by creating a method. The first part should test if the year is less then 1582. If so, return true if it's a multiple of 4. The second part is well described on Wikipedia here. Putting it together gives something like,

private static boolean isLeapYear(int year) {
    if (year < 1582) {
        return (year % 4 == 0);
    }
    /*
     * Rest of algorithm from: http://en.wikipedia.org/wiki/Leap_year
     */
    if (year % 4 != 0) {
        /*
         * if (year is not divisible by 4) then (it is a common year)
         */
        return false;
    } else if (year % 100 != 0) {
        /*
         * else if (year is not divisible by 100) then (it is a leap year)
         */
        return true;
    }
    /*
     * else if (year is not divisible by 400) then (it is a common year)
     * else (it is a leap year)
     */
    return (year % 400 == 0);
}

Then you can use printf to output the result,

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a year: ");
    int year = input.nextInt();
    System.out.printf("%d %s leap year", year, isLeapYear(year) ? "is a"
            : "is not a");
}
if (year < 1582 && year % 4 == 0)
    System.out.println(year + " is a leap year");
else if (year < 1582)
    System.out.println(year + " is not a leap year");
else if (year >= 1582 && (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)))
    System.out.println(year + " is a leap year");
else
    System.out.println(year + " is not a leap year");

jgrasp - Java leap year code problems - Stack Overflow

java jgrasp leap-year
Rectangle 27 0

Firstly, this if((year < 1582) == (year % 4==0)) checks boolean equality. I think you wanted an if((year < 1582) && (year % 4==0)) but I'm afraid that still doesn't fix your logic.

I suggest you start by creating a method. The first part should test if the year is less then 1582. If so, return true if it's a multiple of 4. The second part is well described on Wikipedia here. Putting it together gives something like,

private static boolean isLeapYear(int year) {
    if (year < 1582) {
        return (year % 4 == 0);
    }
    /*
     * Rest of algorithm from: http://en.wikipedia.org/wiki/Leap_year
     */
    if (year % 4 != 0) {
        /*
         * if (year is not divisible by 4) then (it is a common year)
         */
        return false;
    } else if (year % 100 != 0) {
        /*
         * else if (year is not divisible by 100) then (it is a leap year)
         */
        return true;
    }
    /*
     * else if (year is not divisible by 400) then (it is a common year)
     * else (it is a leap year)
     */
    return (year % 400 == 0);
}

Then you can use printf to output the result,

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a year: ");
    int year = input.nextInt();
    System.out.printf("%d %s leap year", year, isLeapYear(year) ? "is a"
            : "is not a");
}
if (year < 1582 && year % 4 == 0)
    System.out.println(year + " is a leap year");
else if (year < 1582)
    System.out.println(year + " is not a leap year");
else if (year >= 1582 && (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)))
    System.out.println(year + " is a leap year");
else
    System.out.println(year + " is not a leap year");

jgrasp - Java leap year code problems - Stack Overflow

java jgrasp leap-year