An integer that meets two conditions can become a leap year:
1. Non-hundred years can be evenly divisible by 4;
2. A hundred years can be evenly divisible by 400.
Java syntax:
year % 4 == 0 && year % 100 != 0 || year % 400 == 0
Example:
package test; import java.util.Scanner; public class LeapYear { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); System.out.println("请输入一个年份:"); long year = scan.nextLong(); //接收用户输入 if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { System.out.println(year + "是闰年!"); } else { System.out.println(year + "不是闰年"); } } }
Recommended tutorial: Java tutorial
The above is the detailed content of How to determine whether a certain year is a leap year in Java. For more information, please follow other related articles on the PHP Chinese website!