
Patrick B. answered 05/21/19
Math and computer tutor/teacher
Here's part of your solution:
private MonthEnd( int month, int year)
{
int iReturn=-1;
switch ( month)
{
//30 days has september, April, June, and November
case 9:
case 4:
case 6:
case 11:
{
iReturn = 30;
break;
}
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
iReturn = 31;
break;
}
case 2: //February : leap year
{
if (
((y % 4)==0) && ((y % 1000) != 0)
)
{
iReturn = 29;
}
else
{
iReturn = 28;
}
break;
}
default
{
System.out.println(" invalid month ");
nreak;
}
} //switch
return( iReturn);
} //MonthEnd
So if your input date is end of month, you can easily find the number of days at the end of the next month using this function twice: once to check if your input date is in fact the last day of the month and if so, the end of month for the next month. Otherwise, you just increment your month number and you're set.