+ Start a Discussion
jason.bradleyjason.bradley 

Find start year of yearly fiscal period based on current date and start month/day of period

Hello,

 

I'm attempting to come up with an algorithm to find the start year of a year long fiscal period that applies to a given date, based on the start month and day of the fiscal period every year.

 

Currently, I calculate the days between the date passed in and the date that the fiscal period will change in the same year. If the number of days is positive (the current date falls within the last fiscal period), then I return the current year - 1. 

Otherwise, I return the current year.

 

Here are the specific methods that I am using to calculate this:

    global Integer getIndexInListStr(List<String> strList, String strToFind)
    {
    	for (Integer j = 0; j < strList.size(); j++)
    	{
    		if (strList.get(j) == strToFind)
    		{
    			return j;
    		}
    	}
    	return null;
    }
    
    global Integer getOffPeriodStartYear(Date d, Integer periodStartMonth, Integer periodStartDay)
    {
    	Date tempPeriodStartDay = Date.newInstance(d.year(), periodStartMonth, periodStartDay);
    	
    	if (d.daysBetween(tempPeriodStartDay) > 0)
    	{
    		return (tempPeriodStartDay.year() - 1);
    	}
    	else
    	{
    		return tempPeriodStartDay.year();
    	}
    }

 

This is where I am calling on these methods in order to find this value:

Fiscal_Period_Year__c = String.valueOf(getOffPeriodStartYear(currentCalendarDate, (getIndexInListStr(monthNames, fiscalStartMonth) + 1), fiscalStartDay));

 

This is all in a loop that is within a batch Apex class, but assume that currentCalendarDate is 2/1/1990, monthNames is just a list full of the names of each month, fiscalStartMonth is February, and fiscalStartDay is the 1st. 

 

When I think through this logically, it would think that it would say that the fiscal period start year on 2/1/1990 is 1990, as it should, but for some reason, it doesn't begin to label the fiscal start year as 1990 instead of 1989 until April 1st. I have not been able to figure out why.

 

Thanks in advance to anyone that can help!