Java Program To Find Leap Year Between Two Dates

Chapter: Miscellaneous Last Updated: 11-08-2023 16:46:43 UTC

Program:

            /* ............... START ............... */
                

import java.util.Scanner;

public class LeapYearFinder {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter the start year: ");
        int startYear = scanner.nextInt();
        
        System.out.print("Enter the end year: ");
        int endYear = scanner.nextInt();
        
        System.out.println("Leap years between " + startYear + " and " + endYear + ":");
        for (int year = startYear; year <= endYear; year++) {
            if (isLeapYear(year)) {
                System.out.println(year);
            }
        }
        
        scanner.close();
    }
    
    public static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
}

                /* ............... END ............... */
        

Output

Enter the start year: 2000
Enter the end year: 2025
Leap years between 2000 and 2025:
2000
2004
2008
2012
2016
2020
2024

In this example, the program identified the leap years between the years 2000 and 2025.
Keep in mind that the output will vary based on the range you provide as input.

Notes:

  • A leap year is a year that has an extra day, February 29th, instead of the usual 28 days. Leap years are typically divisible by 4, except for years that are both divisible by 100 and not divisible by 400.
  • Inside the loop, the isLeapYear method is called to check whether the current year is a leap year. This method takes a year as input and returns true if it's a leap year and false otherwise. The condition (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) checks whether the year is divisible by 4 and not divisible by 100, or if it's divisible by 400.
  • If the current year is determined to be a leap year (according to the isLeapYear method), it's printed to the console.
  • When you run the program and input a start year and an end year, it will calculate and display all the leap years between those years, based on the rules mentioned above.

Tags

Java program to find leap year between two dates #How to find leap year between two years in Java? #Find leap year between two dates Java100, or if it's divisible by 400.

Similar Programs Chapter Last Updated
Find Unique Elements In List Java Miscellaneous 07-10-2023
Java Program To Implement A Custom equals() and hashcode() In Java Miscellaneous 07-10-2023
Java Program To Find The Intersection Of Two HashSets Miscellaneous 07-10-2023
Java Program To Remove Duplicate Elements From List Miscellaneous 07-10-2023
Java program to parse a date and time string from a log file and store it in a database Miscellaneous 19-09-2023
Java Program To Print All The Dates In A Month That Fall On A Weekend Miscellaneous 19-09-2023
Java Program To Find Number Of Working Days In A Month Miscellaneous 19-09-2023
Java Program To Calculate Age From Year Of Birth Miscellaneous 16-09-2023
How To Check If Two Strings Are Anagrams In Java Miscellaneous 22-08-2023
Java Program To Make A Snake Game Miscellaneous 15-08-2023
Java Program To Find Repeated Characters Of String Miscellaneous 15-08-2023
String To Array In Java Miscellaneous 11-08-2023
Java Program To Convert Date To String Miscellaneous 11-08-2023
Java Program To Convert String To Date Object Miscellaneous 11-08-2023
Java Program To Find Number Of Days In A Month Miscellaneous 11-08-2023
Java Program To Print First And Last Day Of Month Miscellaneous 11-08-2023
Java Code To Find Difference Between Two Dates In Years Months And Days Miscellaneous 11-08-2023
Java program to calculate age from year of birth Miscellaneous 29-06-2023
Swap Two Numbers Without Using Third Variable In Java Miscellaneous 02-06-2023
Java Program To Find The Average Of An Array Of Numbers Miscellaneous 02-06-2023
How Do You Find The Factorial Of A Number In Java Miscellaneous 02-06-2023
Java Program That Takes Two Numbers As Input And Prints Their Sum Miscellaneous 27-05-2023
How To Get The Length Of An Array In Java Miscellaneous 27-05-2023
Java Add Element To List Example Miscellaneous 19-05-2023
Java Program To Square All Items In List Miscellaneous 17-05-2023
Java Program To Merge Two Lists Miscellaneous 17-05-2023
How To Reverse A List In Java Miscellaneous 17-05-2023
Java Program To Find Unique Elements In An Array Miscellaneous 14-05-2023
Java Program To List All Elements In List Miscellaneous 30-04-2023
Java Program To Create XML File Miscellaneous 23-04-2023

1 2