SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Difference Between Two Dates Program in Java and Python

10 February 2013

Difference between two dates program with algorithm, explanation, Java solution and simple Python solution for ISC students.

Question:

Write a program to accept two dates in dd/mm/yyyy format and find the difference in days between the two dates. The program should validate the input dates.

INPUT: Enter first date: 20/12/2012 Enter second date: 11/02/2013 OUTPUT: Difference = 54 days

Algorithm:

Step 1: Start.

Step 2: Accept the two dates.

Step 3: Separate day, month and year from both date strings.

Step 4: Convert each date into total number of days counted from year 1.

Step 5: While counting, add 366 for leap years and 365 for ordinary years.

Step 6: Add the completed months and current day for each date.

Step 7: Subtract the two totals and take the positive value.

Step 8: Display the difference in days.

Step 9: Inside the day-count method, use one loop for completed years and another loop for completed months.

Step 10: Use the leap-year method inside both loops so February and full-year totals are counted correctly.

Step 11: Stop.

Explanation:

The main idea is to convert each date into a total day count. Once both dates are represented as numbers, finding the difference becomes a simple subtraction.

The method countDays() counts all full years before the given year. For each previous year, it adds 366 days if it is a leap year and 365 days otherwise.

After counting full years, the method adds the days of all completed months before the given month. February is treated specially in a leap year, because it contributes 29 days instead of 28.

Finally, the day of the current month is added. The difference between the two totals gives the number of days between the dates. If the difference is negative, it is made positive so the answer remains correct even if the dates are entered in reverse order.

The important helper method is the day-count method. It uses the year loop to count completed years, the month loop to count completed months, and then adds the current day, so each date becomes one comparable integer value.

The method uses local variables for day, month and year values after extracting them from the input strings. The loops convert those separate date parts into one counter value, making comparison and subtraction straightforward.

Java Program:

Java
/**
* The class Date_Difference inputs 2 dates and finds the difference between them
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;
class Date_Difference
{
    static
        Scanner sc = new Scanner(System.in);
    int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

    //function for checking for Leap Year

    int isLeap(int y)
    {
        if((y%400==0) || ((y%100!=0)&&(y%4==0)))
        return 29;
        else
        return 28;
    }

    //function for checking date validation

    boolean dateValidate(int d, int m, int y)
    {
        month[2]=isLeap(y);
        if(m<0 || m>12 || d<0 || d>month[m] || y<0 || y>9999)
        return false;
        else
        return true;
    }

    //function for finding day number from year = 1 till the inputted year

    int dayno(int d, int m, int y)
    {
        int dn=0;
        month[2]=isLeap(y);
        for(int i=1;i<m;i++)
        {
            dn=dn+month[i];
        }
        dn=dn+d;
        for(int i=1;i<y;i++)
        {
            if(isLeap(i)==29)
            dn=dn+366;
            else
            dn=dn+365;
        }
        return dn;
    }

    public static void main(String args[])
    {
        Date_Difference ob=new Date_Difference();
        System.out.print("Enter the 1st date in (dd/mm/yyyy) format: ");
        String date1=sc.nextLine().trim();
        int p,q;

        //Extracting the day
        p=date1.indexOf("/");
        int d1=Integer.parseInt(date1.substring(0,p));

        //Extracting the month
        q=date1.lastIndexOf("/");
        int m1=Integer.parseInt(date1.substring(p+1,q));

        //Extracting the year
        int y1=Integer.parseInt(date1.substring(q+1));

        System.out.print("Enter the 2nd date in (dd/mm/yyyy) format: ");
        String date2=sc.nextLine().trim();
        p=date2.indexOf("/");
        int d2=Integer.parseInt(date2.substring(0,p));
        q=date2.lastIndexOf("/");
        int m2=Integer.parseInt(date2.substring(p+1,q));
        int y2=Integer.parseInt(date2.substring(q+1));

        //Validating both the dates

        if(ob.dateValidate(d1,m1,y1)==true && ob.dateValidate(d2,m2,y2)==true)
        {
            int a=ob.dayno(d1,m1,y1);
            int b=ob.dayno(d2,m2,y2);
            System.out.print("Output : Difference = "+Math.abs(a-b)+" days.");
        }
        else
        System.out.println("Invalid Date");
    }
}

Equivalent Python Program:

Python
# Read the date values and separate day, month and year for calculation.
# Helper functions keep repeated calculations separate from the main logic.
# Month lengths and leap-year checks control valid date movement/counting.
# Display the calculated date/day result after all updates are complete.

def is_leap(y):
    return y % 400 == 0 or (y % 100 != 0 and y % 4 == 0)
def count_days(d, m, y):
    month = [0,31,28,31,30,31,30,31,31,30,31,30,31]
    total = d
    for year in range(1, y):
        if is_leap(year):
            total = total + 366
        else:
            total = total + 365
    for i in range(1, m):
        if i == 2 and is_leap(y):
            total = total + 29
        else:
            total = total + month[i]
    return total
date1 = input("Enter first date: ")
date2 = input("Enter second date: ")
p1 = date1.find('/')
q1 = date1.rfind('/')
d1 = int(date1[0:p1])
m1 = int(date1[p1 + 1:q1])
y1 = int(date1[q1 + 1:])
p2 = date2.find('/')
q2 = date2.rfind('/')
d2 = int(date2[0:p2])
m2 = int(date2[p2 + 1:q2])
y2 = int(date2[q2 + 1:])
diff = count_days(d2, m2, y2) - count_days(d1, m1, y1)
if diff < 0:
    diff = -diff
print("Difference =", diff, "days")

Output:

INPUT: Enter first date: 20/12/2012 Enter second date: 11/02/2013 OUTPUT: Difference = 54 days

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →