Home >Java >JavaBase >How to determine whether a certain year is a leap year in Java

How to determine whether a certain year is a leap year in Java

王林
王林Original
2019-11-13 13:17:214656browse

How to determine whether a certain year is a leap year in Java

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is java programNext article:what is java program