Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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

  1. main reads the two dates as three integers each (day, month, year) and creates the objects first and second using the constructor DateDifference(dd, mm, yy), which stores the date and fills the array md with the days of the months of a normal year.
  2. main calls isValid() 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 when isLeap(y) is true.
  3. If either date is invalid, main prints INVALID DATE.
  4. Otherwise main calls first.difference(second), which finds dayCount() of both objects and returns the positive difference.
  5. dayCount() of a date:
    • adds 365 or 366 for each whole year from 1 to y - 1, using isLeap(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.

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

VariableData TypePurpose
dintData member: day of this date
mintData member: month of this date
yintData member: year of this date
mdint[]Data member: days in each month of a normal year, index 0 for January
ddintParameter of the constructor: the day
mmintParameter of the constructor: the month
yyintParameter of the constructor: the year
yrintParameter of isLeap(): the year to test
lenintLength of the month in isValid()
totalintRunning count of days in dayCount()
iintLoop counter over years in one loop and over months in the other, and in isLeap(i) calls
otherDateDifferenceParameter of difference(): the other date
diffintDifference of the day numbers, made positive
scScannerReads the dates typed by the user
aintDay of the first date read in main
bintMonth of the first date read in main
cintYear of the first date read in main
pintDay of the second date read in main
qintMonth of the second date read in main
rintYear of the second date read in main
firstDateDifferenceObject holding the first date
secondDateDifferenceObject holding the second date
argsString[]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: 895

Sample 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: 0

Sample 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: 1

Sample 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: 2

Sample 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: 1

Sample 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: 1

Sample 7

Enter the first date (dd mm yyyy): 31 4 2021
Enter the second date (dd mm yyyy): 1 5 2021
INVALID DATE

Sample 8

Enter the first date (dd mm yyyy): 29 2 1900
Enter the second date (dd mm yyyy): 1 1 2000
INVALID DATE

One value per line

Dry 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$).

ObjectAfter year loopMonths beforeLeap day added+ ddayCount()
first73742431 (January)no, month is not after February28737483
second73742431 + 28 = 59yes, +11737485

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

  1. Day of the year "Read a date and print its day number within that year."
  2. Date after N days "Read a date and a number of days N and print the date after N days."
  3. Day of the week "Read a date and print the day of the week on which it falls."