[SalesForce] How to calculate First of prior quarter and last of prior quarter with current date

There is a requirement, where I have to Calculate First of prior quarter and Last of prior quarter with Today's date.

Jan, Feb, Mar -- 10/1-12/31 of prior year.
Apr, May, Jun -- 1/1-3/31 of current year.
Jul, Aug, Sep -- 4/1-6/30 of current year.
Oct, Nov, Dec -- 7/1-9/30 of current year.

This will give us current month:

Date todayDate = System.today().toStartOfMonth();

This will give us current quarter:

Integer currentQtr = Decimal.valueOf(todayDate.month()).divide(3.0, 0, System.RoundingMode.UP).intValue();

Best Answer

Assuming you're not using the standard Fiscal Year functionality, then the Date class will have all you need as Keith C said.

For example, I would start by defining your date ranges:

Date fiscalQ1Start = date.newInstance(system.today().year().addYears(-1), 10, 1);
Date fiscalQ1End = date.newInstance(system.today().year().addYears(-1), 10, 1);
Date fiscalQ2Start = date.newInstance(system.today().year(), 1, 1);
Date fiscalQ2End = date.newInstance(system.today().year(), 3, 31);

and so on for Q3 & Q4.

Then use Apex logic to calculate which quarter needs to be used etc.