Find number of days between two given dates

You are given two dates 1/1/2015 and 1/1/2017 and are asked to find number of days between them. 

I had written code for the same, the only catch is you are not suppose to use any of java's date functions. 

1. Check for conditions to date is greater and start date. 

2. Consider for the leap year or not this is the major boundary condition. You can have fromDate in january/feb then leap year matters if not leap year for the start year does not matter. Also the similar boundary condition is applicable for toDate, hear again if any date before feb 28 or feb 29 leap year does not matter. 

The interviewer came up with follow up question asking whether if my date range is huge how can you improve the algorithm. One simple solution if you are comparing between centuries then you can store how many days are there in a 100 years and simply add that to your result. 

Author: Balaji Rajagopalan
Login to Answer
Sort By:
My Answer