ISC Practicals and Theory - Date Difference (OOP)
Updated 21 Oct 2026
This program prints the number of days between two dates. In this OOP version one object of the class DateDifference holds one date. A parameterised constructor stores the date, small methods check it and convert it to a day number, and a method difference() compares two objects, so main creates two objects and prints the result.
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 1 January of year 1 (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. A leap year has 29 days in February. So 2000 and 2020 are leap years; 1900 and 2100 are not.
For example, 28 February 2020 and 1 March 2020 are 2 days apart, but 28 February 2100 and 1 March 2100 are 1 day apart.
The two dates may be given in either order; difference() returns the positive difference.
Algorithm
mainreads the two dates as three integers each (day, month, year) and creates the objectsfirstandsecondusing the constructorDateDifference(dd, mm, yy), which stores the date and fills the arraymdwith the days of the months of a normal year.maincallsisValid()on both objects:- the year must be from 1 to 9999 and the month from 1 to 12;
- the day must be from 1 to the month length,
md[m - 1], or 29 for February whenisLeap(y)is true.
- If either date is invalid,
mainprintsINVALID DATE. - Otherwise
maincallsfirst.difference(second), which findsdayCount()of both objects and returns the positive difference. dayCount()of a date:- adds 365 or 366 for each whole year from 1 to
y - 1, usingisLeap(i); - adds the days of the months before month
m; - adds 1 if the month is after February and the year is a leap year;
- adds
d.
- adds 365 or 366 for each whole year from 1 to
Program
Each method does one job: isLeap() and isValid() return a boolean, dayCount() and difference() return an int.
import java.util.Scanner;
/**
* Class DateDifference (OOP version)
* One object stores one date. The difference between two dates is found by
* converting each date to a day number (days counted from 1 January of year 1)
* and subtracting.
*/
public class DateDifference
{
int d, m, y; // day, month and year of this date
int md[]; // days in each month of a non-leap year (index 0 = January)
/**
* Parameterised constructor: stores the date and fills the month-length array.
* @param dd day, mm month, yy year
*/
DateDifference(int dd, int mm, int yy)
{
d = dd;
m = mm;
y = yy;
md = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
}
/**
* Checks whether a year is a leap year.
* @param yr the year
* @return true if yr is divisible by 4 and not by 100, or divisible by 400
*/
boolean isLeap(int yr)
{
return (yr % 4 == 0 && yr % 100 != 0) || (yr % 400 == 0);
}
/**
* Checks whether this date is valid.
* @return true if year is 1 to 9999, month is 1 to 12 and day is within the month
*/
boolean isValid()
{
if (y < 1 || y > 9999 || m < 1 || m > 12)
return false;
int len = md[m - 1]; // length of the month in a normal year
if (m == 2 && isLeap(y))
len = 29; // February has 29 days in a leap year
return d >= 1 && d <= len;
}
/**
* Converts this (valid) date to a day number counted from 1 January of
* year 1 (1 January of year 1 is day 1).
* @return the day number of this date
*/
int dayCount()
{
int total = 0;
for (int i = 1; i < y; i++) // whole years before year y
{
if (isLeap(i))
total = total + 366;
else
total = total + 365;
}
for (int i = 0; i < m - 1; i++) // whole months before month m
{
total = total + md[i];
}
if (m > 2 && isLeap(y))
total = total + 1; // leap day already passed this year
return total + d; // add the days of the current month
}
/**
* Finds the number of days between this date and another date.
* @param other the other date
* @return the absolute difference of the two day numbers
*/
int difference(DateDifference other)
{
int diff = dayCount() - other.dayCount();
if (diff < 0)
diff = -diff; // absolute difference: order does not matter
return diff;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first date (dd mm yyyy): ");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
System.out.print("Enter the second date (dd mm yyyy): ");
int p = sc.nextInt();
int q = sc.nextInt();
int r = sc.nextInt();
DateDifference first = new DateDifference(a, b, c);
DateDifference second = new DateDifference(p, q, r);
if (first.isValid() && second.isValid())
System.out.println("Number of days between the dates: " + first.difference(second));
else
System.out.println("INVALID DATE");
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
d | int | Data member: day of this date |
m | int | Data member: month of this date |
y | int | Data member: year of this date |
md | int[] | Data member: days in each month of a normal year, index 0 for January |
dd | int | Parameter of the constructor: the day |
mm | int | Parameter of the constructor: the month |
yy | int | Parameter of the constructor: the year |
yr | int | Parameter of isLeap(): the year to test |
len | int | Length of the month in isValid() |
total | int | Running count of days in dayCount() |
i | int | Loop counter over years in one loop and over months in the other, and in isLeap(i) calls |
other | DateDifference | Parameter of difference(): the other date |
diff | int | Difference of the day numbers, made positive |
sc | Scanner | Reads the dates typed by the user |
a | int | Day of the first date read in main |
b | int | Month of the first date read in main |
c | int | Year of the first date read in main |
p | int | Day of the second date read in main |
q | int | Month of the second date read in main |
r | int | Year of the second date read in main |
first | DateDifference | Object holding the first date |
second | DateDifference | Object holding the second date |
args | String[] | Command-line arguments of main (not used) |
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. The objects are first (28, 2, 2020) and second (1, 3, 2020); isValid() is true for both. In dayCount() the year loop adds the days of years 1 to 2019: $2019 \times 365 + 489 = 737424$ days, where 489 is the number of leap years from 1 to 2019 ($504 - 20 + 5$).
| Object | After year loop | Months before | Leap day added | + d | dayCount() |
|---|---|---|---|---|---|
first | 737424 | 31 (January) | no, month is not after February | 28 | 737483 |
second | 737424 | 31 + 28 = 59 | yes, +1 | 1 | 737485 |
In first.difference(second): diff = 737483 - 737485 = -2, made positive, so the method returns 2. main 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."
- 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."