0

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();
2
  • I suggest you have a go at writing this yourself - the Apex Date class has all the methods you will need.
    – Keith C
    Commented Sep 3, 2014 at 15:05
  • By using Date class methods, I can calculate current month and current quarter. But, I am not getting the logic how to calculate the Previous quarter First Date and last date. Commented Sep 3, 2014 at 15:19

2 Answers 2

2

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.

0

This is the answer for above question:

            Date startDate;
            Date endDate;
            Date todayDate= System.today().toStartOfMonth();            
            Integer currentQtr = Decimal.valueOf(todayDate.month()).divide(3.0, 0, System.RoundingMode.UP).intValue();           
            Date startOfYear = date.newInstance(system.today().year(), 1, 1);
            if(currentQtr == 1) {
                startDate = startOfYear.addMonths(-3);
            } else if(currentQtr == 2) {
                startDate = startOfYear;                
            } else if(currentQtr == 3) {
                startDate = startOfYear.addMonths(3);
            } else if(currentQtr == 4) {           
                startDate = startOfYear.addMonths(6);
            }
            endDate = startDate.addMonths(3) - 1;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .