ISC Practicals and Theory - Date Difference
Updated 21 Oct 2026
This program reads two dates and prints how many days lie between them. Each date is turned into a single day number, so the answer is just a subtraction. This version is the straightforward one: everything is written in the main method, with the same steps applied to each of the two dates in a loop.
Definition
The number of days between two dates is the difference of their day numbers, where the day number of a date is the count of days from a fixed starting day (here 1 January of year 1, which is day 1) up to that date.
A year is a leap year if it is divisible by 4 and not by 100, or if it is divisible by 400. So 2020 and 2000 are leap years, while 2021 and 2100 are not. A leap year has 29 days in February.
For example, 28 February 2020 and 1 March 2020 are 2 days apart (29 February lies between them), but 28 February 2021 and 1 March 2021 are only 1 day apart.
The two dates may be given in either order; the program prints the positive difference.
Algorithm
- Read the two dates, each as day, month and year, into the arrays
d,mandy(index 0 is the first date, index 1 the second). - For each date, check that it is valid: the year from 1 to 9999, the month from 1 to 12 and the day from 1 to the length of the month. The array
mdholds the days of the months of a normal year; February has 29 days in a leap year (divisible by 4 and not by 100, or divisible by 400). If either date is invalid,okis false. - For a valid date, find its day number
dn[k]: add 365 or 366 days for every whole year before the year, the days of every whole month before the month (plus 1 if the month is after February in a leap year), and then the day of the month. - Subtract the two day numbers and make the difference positive, so the order of the dates does not matter.
- Print the number of days, or
INVALID DATEif either date is invalid.
Program
Everything is written inside main: one loop reads the dates and another checks each date and works out its day number; the difference is then a subtraction.
import java.util.Scanner;
public class DateDifference
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int d[] = new int[2]; // days of the two dates
int m[] = new int[2]; // months of the two dates
int y[] = new int[2]; // years of the two dates
int md[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // days in each month of a non-leap year (index 0 = January)
for (int k = 0; k < 2; k++)
{
if (k == 0)
System.out.print("Enter the first date (dd mm yyyy): ");
else
System.out.print("Enter the second date (dd mm yyyy): ");
d[k] = sc.nextInt();
m[k] = sc.nextInt();
y[k] = sc.nextInt();
}
boolean ok = true; // true while both dates are valid
int dn[] = new int[2]; // day number of each date, counted from year 1
for (int k = 0; k < 2; k++)
{
boolean leap = (y[k] % 4 == 0 && y[k] % 100 != 0) || (y[k] % 400 == 0); // leap year?
if (y[k] < 1 || y[k] > 9999 || m[k] < 1 || m[k] > 12)
{
ok = false;
}
else
{
int len = md[m[k] - 1]; // length of the month in a normal year
if (m[k] == 2 && leap)
len = 29; // February has 29 days in a leap year
if (d[k] < 1 || d[k] > len)
ok = false;
}
if (ok)
{
int total = 0;
for (int i = 1; i < y[k]; i++) // whole years before year y
{
if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0))
total = total + 366;
else
total = total + 365;
}
for (int i = 0; i < m[k] - 1; i++) // whole months before month m
{
total = total + md[i];
}
if (m[k] > 2 && leap)
total = total + 1; // leap day already passed this year
dn[k] = total + d[k]; // add the days of the current month
}
}
if (ok)
{
int diff = dn[0] - dn[1];
if (diff < 0)
diff = -diff; // absolute difference: order does not matter
System.out.println("Number of days between the dates: " + diff);
}
else
{
System.out.println("INVALID DATE");
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the numbers typed by the user |
d | int[] | Days of the two dates |
m | int[] | Months of the two dates |
y | int[] | Years of the two dates |
md | int[] | Days in each month of a non-leap year, index 0 = January |
k | int | Index of the date being read or worked on, 0 or 1 |
ok | boolean | True while both dates are valid |
dn | int[] | The day number of each date, counted from year 1 |
leap | boolean | True if the year of the date being worked on is a leap year |
len | int | Length of the month of the date being checked |
total | int | Running count of days while the day number is built |
i | int | Loop counter over the whole years and then the whole months before the date |
diff | int | The positive difference between the two day numbers |
Sample input and output
Sample 1
Enter the first date (dd mm yyyy): 15 8 1947
Enter the second date (dd mm yyyy): 26 1 1950
Number of days between the dates: 895Sample 2
Enter the first date (dd mm yyyy): 1 1 2020
Enter the second date (dd mm yyyy): 1 1 2020
Number of days between the dates: 0Sample 3
Enter the first date (dd mm yyyy): 28 2 2021
Enter the second date (dd mm yyyy): 1 3 2021
Number of days between the dates: 1Sample 4
Enter the first date (dd mm yyyy): 28 2 2020
Enter the second date (dd mm yyyy): 1 3 2020
Number of days between the dates: 2Sample 5
Enter the first date (dd mm yyyy): 28 2 2100
Enter the second date (dd mm yyyy): 1 3 2100
Number of days between the dates: 1Sample 6
Enter the first date (dd mm yyyy): 1 3 2021
Enter the second date (dd mm yyyy): 28 2 2021
Number of days between the dates: 1Sample 7
Enter the first date (dd mm yyyy): 31 4 2021
Enter the second date (dd mm yyyy): 1 5 2021
INVALID DATESample 8
Enter the first date (dd mm yyyy): 29 2 1900
Enter the second date (dd mm yyyy): 1 1 2000
INVALID DATEDry run
Take the input 28 2 2020 and 1 3 2020. Both dates are valid (2020 is a leap year, so February may have 29 days). In the day-number loop the year loop adds the days of years 1 to 2019: that is $2019 \times 365 + 489 = 737424$ days, where 489 is the number of leap years from 1 to 2019 ($504 - 20 + 5$). This part is the same for both dates.
| Date | After year loop | Months before | Leap day added | + d | Day number |
|---|---|---|---|---|---|
| 28 2 2020 | 737424 | 31 (January) | no, month is not after February | 28 | 737483 |
| 1 3 2020 | 737424 | 31 + 28 = 59 | yes, +1 | 1 | 737485 |
So dn[0] = 737483, dn[1] = 737485 and diff = 737483 - 737485 = -2, which is made positive: 2. The program prints Number of days between the dates: 2, as in the fourth sample run.
Variations
- Day of the year "Read a date and print its day number within that year, for example 1 March in a leap year is day 61."
- Date after N days "Read a date and a number of days N and print the date after N days."
- Day of the week "Read a date and print the day of the week on which it falls."