Write a Java program to count the number of days between two given years


This Java program takes input for a starting year and an ending year from the user and prints the number of days in each year.

The program first prompts the user to enter the starting year and ending year. It then checks if the ending year is greater than the starting year, and if so, it prints the year and the number of days in each year in the range from the starting year to the ending year using a for loop.

The program also includes two additional methods: num_of_Days(int year) and leap_Year(int year). The num_of_Days method takes an input year and returns the number of days in that year based on whether it is a leap year or not. The leap_Year method takes an input year and returns true if it is a leap year, false otherwise.

Source Code

import java.util.Scanner;
class NoDays_TwoYears
{
	public static void main(String[] args)
	{
		System.out.print("Enter The Start Year :");
		Scanner s = new Scanner(System.in);
		int fy = s.nextInt();
		System.out.print("Enter The End Year :");
		s = new Scanner(System.in);
		int ey = s.nextInt();
		if (ey > fy)
		{
			System.out.println("\nYear & Number of Days " );
			for (int i = fy; i <= ey; i++)
			{
				System.out.println( i + " = " + num_of_Days(i));
			}
		}
		else
		{
			System.out.println("End Year must be Greater than First Year ..");
		}
	}
	public static int num_of_Days(int year)
	{
		if (leap_Year(year))
			return 366;
		else
			return 365;
	}
	public static boolean leap_Year(int year)
	{
		return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
	}
}
 

Output

Enter The Start Year :2014
Enter The End Year :2022

Year & Number of Days
2014 = 365
2015 = 365
2016 = 366
2017 = 365
2018 = 365
2019 = 365
2020 = 366
2021 = 365
2022 = 365

Example Programs