Home > Java > JavaBase > body text

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

王林
Release: 2019-11-13 13:17:21
Original
4604 people have browsed it

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
Copy after login

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 + "不是闰年");
             }
       }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!