Tag Archives: loop

Duration between two dates

In this post I will discuss a method to find the duration between two dates. This problem had been asked in ISC Computer Practical in the year 2000. For the sake of simplicity we will assume that the second date comes after the first date in chronological order.

The problem essentially is to count total number of days by adding number of complete months in the time interval given and then adjust the days in the first and the last month of the given date. An integer array days will be used to store the number of days in each month of the year. We will update the second location (index 1) of the array days to 28 or 29 depending on whether the concerned year is a leap year or not.

Determination of leap year is a very important aspect in these type of programs and contrary to the popular belief leap years are not the years which are completely divisible by 4. Those who are working on UNIX or Linux can easily verify the same by typing cal 2 1900 in the terminal window. The command will display the second month (February) of the year 1900 and one can see that the number of days in February 1900 is 28 and not 29! Those who are using an Operating System which doesn’t have the cal command available can visit this URL to confirm the same.

The correct method to test whether any given year is a leap or not is to check for divisibility by 400 in case the year is divisible by 100 and to check for divisibility by 4 in case the year is not divisible by 100.

Using the above method the year 1900 should be checked for divisibility by 400 since year 1900 is divisible by 100 and the year 2000 should be checked for divisibility by 4 since year 2000 is not divisible by 100. Most of us do not know this rule because we usually deal in recent past and recent future. The last special case was the year 1900 which passed a long time back, the next special case would be 2100 which is almost 85 years away!

The program incorporating the above logic would be as follows:

class Calendar{
    public static boolean isLeap(int year){
        int divisor=year%100==0?400:4;
        return year%divisor==0;
    }
    public static int differenceBetweenDates(int d1, int m1, int y1, int d2, int m2, int y2){
        int[] days={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int yearDifference = y2 - y1;
        int duration=0;
        days[1] = isLeap(y2)? 29: 28;
        for(int i=0; i $lt; yearDifference; i++){
            for(int x=0; x $lt; m2; x++){
                duration += days[x];
            }
            m2 = 12;
        }
        days[1] = isLeap(y1)? 29: 28;
        for(int i=m1; i < m2; i++){
            duration += days[i];
        }
        duration += d2-d1;
        return duration;
    }

    public static void main(String []args){
        System.out.println(differenceBetweenDates(1,1,1900, 15,2,2014));
    }
}

In the above program line no. 11 and line no. 17 are for adjusting the element corresponding array element to 29 or 28 depending on whether it’s a leap year or not. The first loop is for calculating the number of days on the basis of number of complete month during the given time period and the last loop is for adjusting the result for the  initial and the final days. Students must pay special attention to the function isLeap() because it may be useful in other date related problems.

As usual, the main function has purposely been kept to minimum and its expected that students will write appropriate input/output statements as per the requirement.