2024年は閏年なので、うるう年の判定ロジック。
本来は愚直にやらないで、ライブラリを利用した方がいいので参考までに。
すべてのうるう年の条件。
- 世紀年とは、00で終わる年のことである。世紀年は、400で割り切れる場合のみ閏年となる。
- 閏年(世紀年を除く)は、4で正確に割り切れる場合に識別できる。
- 世紀年は4と100で割り切れる。
- 非世紀年は4で割り切れるだけである。
// Java program to find a leap year
// Importing Classes/Files
import java.io.*;
// Class for leap-year dealing
public class GeeksforGeeks {
// Method to check leap year
public static void isLeapYear(int year)
{
// flag to take a non-leap year by default
boolean is_leap_year = false;
// If year is divisible by 4
if (year % 4 == 0) {
is_leap_year = true;
// To identify whether it is a
// century year or not
if (year % 100 == 0) {
// Checking if year is divisible by 400
// therefore century leap year
if (year % 400 == 0)
is_leap_year = true;
else
is_leap_year = false;
}
}
// We land here when corresponding if fails
// If year is not divisible by 4
else
// Flag dealing- Non leap-year
is_leap_year = false;
if (!is_leap_year)
System.out.println(year + " : Non Leap-year");
else
System.out.println(year + " : Leap-year");
}
// Driver Code
public static void main(String[] args)
{
// Calling our function by
// passing century year not divisible by 400
isLeapYear(2024);
// Calling our function by
// passing Non-century year
isLeapYear(2025);
}
}
結果の出力
2024 : Leap-year
2025 : Non Leap-year