Nächstes Schaltjahr mit Java berechnen?

2 Antworten

 /**
* checks if a given year is a leap year
*
* @param year
* the year to check
* @return true if the {@code year} is a leap year, false otherwise
*/
private static boolean isLeapYear(int year) {

if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
return true;
else
return false;

}

Du kannst nun selbst schauen, in wie fern sich die Funktionen unterscheiden.

Ganz primitiv könntest du die Jahreszahl einfach so lange hochzählen, bis sie ein gültiges Schaltjahr ist. Das gibst du dann zurück:

private static boolean isLeapYear(int year) {
return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
}

private static int nearestLeapYear(int year) {
while (!isLeapYear(year)) year++;
return year;
}
public static void main(String[] args) {
System.out.println(nearestLeapYear(2015)); // -> 2016
}