the reason you have bugs is because of wrong computation.
your method
public static int computeDaysLeftInYear(int year1,int month1, int day1) actually works without error if and only it is used by getting the days left in a year. but when you incorporate this method with method
computeDaysFromYearsInbetween, it does a wrong computation. Since you want some advice, I'll give you some advice.
I debug your code and trouble some bugs, this is what i noticed:
your first switch statement in
computeDaysLeftInYear method is useless (if incorporate with method
computeDaysFromYearsInbetween)
it would be better to remove that switch.
But how would you get the number of days to be subtracted?
just subtract the
day1 value to your
daysInYearOne variable which have initial value of zero.
int daysInYearOne = 0 - day1;
therefore you will have a negative value or zero. then add it to the number of days a month has to get the remaining days.
exmaple: January 30 will make your
daysInYearOne = -30 then add 31 (January has 31 days) it will make your
daysInYearOne = 1 in the second switch statement if you follow my advice below
then in the second
switch statement, just add another case,
case 1 for first month of the year.
case 1:
daysInYearOne += 31;
then remove the additional value to your switch
switch (month1 + 1) should be
switch (month1)
the other bug you have is in method
computeDaysBeforeDate.
actually you just have a wrong condition in your loop.
for(int i=year1+1;i<=year2-1;i++) {
if(DaysBetweenDates.isLeapyear(i)) daysInbetweenYears+=366;
else daysInbetweenYears+=365;
}
review the condition and initialization.
Actually, for me, there is a better way of doing that than using switch statement, bu that is fine.
to make your activity more challenging, try to add some verification and error messages, for example getting a month that is higher than 12, or date that is beyond 31 or beyond 28 for February.