Back
I want my Java program to calculate if a year is a leap year or not.
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");elseSystem.out.println(year+"is not a leap year");}}
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.
31k questions
32.8k answers
501 comments
693 users