Michael James - 17 days ago 5
Java Question

# Java daysPassed method

I have this method in this java program that I am working on for school. The method works fine, but I was wondering if there is way to make it shorter. Any advice would be great. thanks

``````public int calculateDate(int month, int day, int year, int numOfDays)
``````

{

``````if((daysPassed(month,day,year) + numOfDays) > 0 && (daysPassed(month,day,year) + numOfDays) < 32)
{
dMonth = 1;
dDay = daysPassed(month,day,year) + numOfDays;
} // end of if

if(((daysPassed(month,day,year) + numOfDays) > 31 && (daysPassed(month,day,year) + numOfDays < 61)) && isLeapYear(year) == true)
{
dMonth = 2;
dDay = daysPassed(month,day,year) + numOfDays - 31;
}// end of if

if((daysPassed(month,day,year) + numOfDays) > 31 && (daysPassed(month,day,year) + numOfDays) < 60)
{
dMonth = 2;
dDay = daysPassed(month,day,year) + numOfDays - 31;
}// end of if

if(((daysPassed(month,day,year) + numOfDays) > 60 && (daysPassed(month,day,year) + numOfDays < 92)) && isLeapYear(year) == true){
dMonth = 3;
dDay = daysPassed(month,day,year) + numOfDays - 31 - 29;
}// end of if

if((daysPassed(month,day,year) + numOfDays) > 59 && (daysPassed(month,day,year) + numOfDays) < 91)
{
dMonth = 3;
dDay = daysPassed(month,day,year) + numOfDays - 31 - 28;
}// end of if

if(((daysPassed(month,day,year) + numOfDays) > 91 && (daysPassed(month,day,year) + numOfDays < 122)) && isLeapYear(year) == true)
{
dMonth = 4;
dDay = daysPassed(month,day,year) + numOfDays - (2 * 31) - 29;
}// end of if

if((daysPassed(month,day,year) + numOfDays) > 90 && (daysPassed(month,day,year) + numOfDays) < 121)
{
dMonth = 4;
dDay = daysPassed(month,day,year) + numOfDays - (2 * 31) - 28;
}// end of if

if(((daysPassed(month,day,year) + numOfDays) > 121 && (daysPassed(month,day,year) + numOfDays < 153)) && isLeapYear(year) == true){
dMonth = 5;
dDay = daysPassed(month,day,year) + numOfDays - (2 * 31) - 29 - 30;
}// end of if

if((daysPassed(month,day,year) + numOfDays) > 120 && (daysPassed(month,day,year) + numOfDays) < 152)
{
dMonth = 5;
dDay = daysPassed(month,day,year) + numOfDays - (2 * 31) - 28 - 30;
}// end of if

if(((daysPassed(month,day,year) + numOfDays) > 152 && (daysPassed(month,day,year) + numOfDays < 183)) && isLeapYear(year) == true){
dMonth = 6;
dDay = daysPassed(month,day,year) + numOfDays - (3 * 31) - 29 - 30;
}// end of if

if((daysPassed(month,day,year) + numOfDays) > 151 && (daysPassed(month,day,year) + numOfDays) < 182)
{
dMonth = 6;
dDay = daysPassed(month,day,year) + numOfDays - (3 * 31) - 28 - 30;
}// end of if

if(((daysPassed(month,day,year) + numOfDays) > 182 && (daysPassed(month,day,year) + numOfDays < 214)) && isLeapYear(year) == true)
{
dMonth = 7;
dDay = daysPassed(month,day,year) + numOfDays - (3 * 31) - 29 - (2 * 30);
}// end of if

if((daysPassed(month,day,year) + numOfDays) > 181 && (daysPassed(month,day,year) + numOfDays) < 213)
{
dMonth = 7;
dDay = daysPassed(month,day,year) + numOfDays - (3 * 31) - 28 - (2 * 30);
}// end of if

if(((daysPassed(month,day,year) + numOfDays) > 213 && (daysPassed(month,day,year) + numOfDays < 245)) && isLeapYear(year) == true)
{
dMonth = 8;
dDay = daysPassed(month,day,year) + numOfDays - (4 * 31) - 29 - (2 * 30);
}// end of if

if((daysPassed(month,day,year) + numOfDays) > 212 && (daysPassed(month,day,year) + numOfDays) < 244)
{
dMonth = 8;
dDay = daysPassed(month,day,year) + numOfDays - (4 * 31) - 28 - (2 * 30);
}// end of if

if(((daysPassed(month,day,year) + numOfDays) > 244 && (daysPassed(month,day,year) + numOfDays < 275)) && isLeapYear(year) == true)
{
dMonth = 9;
dDay = daysPassed(month,day,year) + numOfDays - (5 * 31) - 29 - (2 * 30);
}// end of if

if((daysPassed(month,day,year) + numOfDays) > 243 && (daysPassed(month,day,year) + numOfDays) < 274)
{
dMonth = 9;
dDay = daysPassed(month,day,year) + numOfDays - (5 * 31) - 28 - (2 * 30);
}// end of if

if(((daysPassed(month,day,year) + numOfDays) > 274 && (daysPassed(month,day,year) + numOfDays < 306)) && isLeapYear(year) == true)
{
dMonth = 10;
dDay = daysPassed(month,day,year) + numOfDays - (5 * 31) - 29 - (3 * 30);
}// end of if

if((daysPassed(month,day,year) + numOfDays) > 273 && (daysPassed(month,day,year) + numOfDays) < 305)
{
dMonth = 10;
dDay = daysPassed(month,day,year) + numOfDays - (5 * 31) - 28 - (3 * 30);
}// end of if

if(((daysPassed(month,day,year) + numOfDays) > 305 && (daysPassed(month,day,year) + numOfDays < 336)) && isLeapYear(year) == true)
{
dMonth = 11;
dDay = daysPassed(month,day,year) + numOfDays - (6 * 31) - 29 - (3 * 30);
}// end of if

if((daysPassed(month,day,year) + numOfDays) > 304 && (daysPassed(month,day,year) + numOfDays) < 335)
{
dMonth = 11;
dDay = daysPassed(month,day,year) + numOfDays - (6 * 31) - 28 - (3 * 30);
}// end of if

if(((daysPassed(month,day,year) + numOfDays) > 335 && (daysPassed(month,day,year) + numOfDays < 367)) && isLeapYear(year) == true)
{
dMonth = 12;
dDay = daysPassed(month,day,year) + numOfDays - (6 * 31) - 29 - (4 * 30);
}// end of if

if((daysPassed(month,day,year) + numOfDays) > 334 && (daysPassed(month,day,year) + numOfDays) < 366)
{
dMonth = 12;
dDay = daysPassed(month,day,year) + numOfDays - (6 * 31) - 28 - (4 * 30);
}// end of if

return dDay;
``````

}// end of calulateDateenter code here

Assuming you are using Java 8+, you might use `LocalDate` like

``````public int calculateDate(int month, int day, int year, int numOfDays) {
return LocalDate.of(year, month, day).plusDays(numOfDays).getDayOfMonth();
}
``````

If you actually need to set `dMonth` (and `dDay`) too, you could do that as well

``````public int calculateDate(int month, int day, int year, int numOfDays) {
LocalDate ld = LocalDate.of(year, month, day).plusDays(numOfDays);
dMonth = ld.getMonthValue();
dDay = ld.getDayOfMonth();
return dDay;
}
``````
Source (Stackoverflow)