Days between 2 given dates

I have been given a task to calculate the number of days between 2 given dates and have found a really strange result (2127 days) for Input Line 3 which seems to be correct but the given output expected for this task is a different result (1979 days). I've looked at this post Calculate the number of days between two dates along with several other posts on here and used the Joda library as advised and get a result of 2127 days.

*Problem: Given two dates (during the years 1901 and 2999, inclusive), find the number of days between the two dates.

Input: Six sets of data. Each set has two dates in the format month, day, year. For example, the input line '6, 2, 1983, 6, 22, 1983' represents June 2, 1983 to June 22, 1983.

Output: The number of days between the given dates, excluding the starting date and the ending date. For example, there are 19 days between June 2, 1983 and June 22, 1983; namely, June 3, 4, ..., 21.

Sample Input (3 sets):

 Input Line #1:  6, 2, 1983, 6, 22, 1983
 Output #1:  19

Input Line #2:  7, 4, 1984, 12, 25, 1984
 Output #2:  173

 Input Line #3:  1, 3, 1989, 3, 8, 1983
 Output #3:  1979 

Here is my solution

private boolean isYearValid(int year){
    return year >=1901 && year <= 2999;
}

public int numOfDays(String dates){
    Calendar date1 = new GregorianCalendar();
    Calendar date2 = new GregorianCalendar();
    String [] dateSplit = dates.split(",");
    int len = dateSplit.length;
    int year = 0;
    for(int i=0;i<len;i++){
        dateSplit[i]=dateSplit[i].trim();
        if(i==2||i==5){
            try {
                year = Integer.parseInt(dateSplit[i]);
            }
            catch (NumberFormatException e){
                throw new IllegalArgumentException(String.format("Usage: Year input %s is not valid",dateSplit[i]));
            }
            if(!isYearValid(year))
                throw new IllegalArgumentException("Usage: Year of date should be between the years 1901 and 2999, inclusive");
        }
    }
    int [] d = new int[6];

    for(int i=0;i<6;i++)
        try {
            d[i]=Integer.parseInt(dateSplit[i]);
        }
    catch(NumberFormatException e){
        throw new IllegalArgumentException("Usage: Date entered is not valid");
    }
    date1.set(d[2], d[0],d[1]);
    date2.set(d[5], d[3], d[4]);
    long milli1= date1.getTimeInMillis();
    long milli2 = date2.getTimeInMillis();
    long diff;
    if(milli1>milli2){
        diff = milli1 - milli2;
    }
    else{
        diff = milli2 - milli1;
    }
    return (int) (diff / (24 * 60 * 60 * 1000))-1;
}

Solved - Seems like the test data was wrong since 1, 3, 1989, 8, 3, 1983 produces 1979 days.

Answers


Your test data is incorrect. See: http://www.timeanddate.com/date/durationresult.html.


Out of curiosity, is this homework? If not, why not just use Joda-Time library which does this for you. It takes care of the length of the month, leap years, etc....

    DateTime dt1 = ...;
    DateTime dt2 = ...;
    Duration duration = new Duration(dt1, dt2);
    long days = duration.getStandardDays();

Need Your Help

How to install NodeJs older version (0.10.40)

node.js meteor centos

I need to deploy meteor app. Meteor require Node 0.10.40

Calculating frequency of symbols of binary file in c# not working but working for equivalent c++ code

c# c++ algorithm linked-list binaryfiles

I am trying to calculate the frequency of symbols in a binary file in c#, I have already done that in c++ and that works fine and i have switched to c# from c++ because i have to implement the same...