public class LeapYear {
public static void main(String[] args) {
int year = Integer.parseInt(args[0]);
boolean isLeapYear;
// divisible by 4
isLeapYear = (year % 4 == 0);
// divisible by 4 and not 100
isLeapYear = isLeapYear && (year % 100 != 0);
// divisible by 4 and not 100 unless divisible by 400
isLeapYear = isLeapYear || (year % 400 == 0);
System.out.println(isLeapYear);
}
}
I am passing 1900 as my input. The first condition evaluates to true as its divisible by 4, but again 1900 should be divisible by 100 too…
How come i am getting 1900 as not a leap year…. what is the value being passed in the second && condition… (year % 100 !=0)
Update
public class TestSample {
public static void main(String[] args){
int leapYear = Integer.parseInt(args[0]);
boolean isLeapYear;
isLeapYear = (leapYear % 4 == 0) && (leapYear % 100 != 0);
System.out.println("Its Leap Year" +isLeapYear);
}
}
Compiling this program prints 1900 not a leapYear How???? Here i am not even checking for the whether its divisible by 400 or not.
To explain your code:
Another suggestion I have for you is to just use the GregorianCalendar to find what you want: