Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8240301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:26:39+00:00 2026-06-07T20:26:39+00:00

I have been given a task to calculate the number of days between 2

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:26:40+00:00Added an answer on June 7, 2026 at 8:26 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been given a bit of a strange task, there are around 1500-2000
I have been given a task to create a bridge between C# and Java.
I have been given the task of adding functionality to an existing IIS 6.0
I have been given the task to design a database to store a lot
I have been given the task of re-writing some libraries written in C# so
I have been given the task of devising a custom forms manager that has
I have been given the task of converting a macromedia application to a web
I am a student on a work placement. I have been given the task
I work in the Systems & admin team and have been given the task
I have been given the unenviable task of cleaning up after a developer who

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.