Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

I want my Java program to calculate if a year is a leap year or not.

1 Answer

0 votes
by (13.1k points)

You can try like this:

import java.util.Calendar;

public class Main

{

    public static boolean isLeapyear(int year){

        Calendar cal=Calendar.getInstance();

        cal.set(Calendar.YEAR,year);

        return cal.getActualMaximum(Calendar.DAY_OF_YEAR) > 365;

    }

public static void main(String[] args) {

int year=2020;

if(isLeapyear(year))

System.out.println(year+" is a leap year");

else

System.out.println(year+"is not a leap year");

}

}

Want to learn Java? Check out the Java certification from Intellipaat. 

Related questions

0 votes
1 answer
asked Feb 17, 2021 in Java by Harsh (1.5k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...