How to get end date for Current Fiscal Quarter in apex

Here is the simplest way I getting date I believe - We just need to query Period Object record and thats it. I think most of us are not aware of this Object in salesforce.com

Date closeDate = [Select EndDate From Period Where type = 'Quarter' and StartDate = THIS_FISCAL_QUARTER].EndDate;

Try the following, I just wrote it and tested for our fiscal year, worked ok...

Integer FiscalYearStartMonth = [select FiscalYearStartMonth from Organization where id=:Userinfo.getOrganizationId()].FiscalYearStartMonth;

Date fiscalYearStartDate;
Integer quarter;

if(system.today().month() >= FiscalYearStartMonth)
{
    fiscalYearStartDate = date.newinstance(system.today().year(), FiscalYearStartMonth, 1);
    quarter = ((system.today().month() - FiscalYearStartMonth) / 3) + 1;
}
else
{
    fiscalYearStartDate = date.newinstance(system.today().year() - 1, FiscalYearStartMonth, 1);
    quarter = ((12 + system.today().month() - FiscalYearStartMonth) / 3) + 1;
}    

Integer addMonths = quarter * 3;

Date lastDateOfThisQuarter = fiscalYearStartDate;

// this is the last date of the current quarter
lastDateOfThisQuarter = lastDateOfThisQuarter.addMonths(addMonths).addDays(-1);

//for standard calendar year

Date dt = date.newInstance(1014,12, 17);
Integer currentMnt =dt.month();
Integer currentQ =((currentMnt-1)/3) + 1;
Date endOfQDate = date.newInstance(dt.year(),currentMnt + (4 - (currentMnt - ((currentQ -1)*3))) , 1).addDays(-1);
system.debug(endOfQDate);

Tags:

Datetime

Apex