1

when i write a trigger I have facing a issue related to quarter. In apex code i want to check particular given date is Which Quarter.please anyone suggests...

2
  • check this out.. salesforce.stackexchange.com/questions/4166/…
    – Ratan Paul
    Commented Feb 5, 2016 at 5:11
  • 1
    the answer depends on whether you are using calendar year fiscal quarters or some the quarters are not based on calendar year (e.g. Q1 starts on Feb 1). You can also use formula fields for this
    – cropredy
    Commented Feb 8, 2016 at 6:52

2 Answers 2

2

You can use Date.Month() for the the month value, here you will get integer value like 1 for Jan...12 for Dec then from this value you can easily get its quarter. Here is example for you:

Integer quarter = date.today().month()/3;
if(quarter == 0)
    system.debug('==FirstQuarter==';
else if(quarter == 1)
    system.debug('==SecondQuarter==');
else if (quarter == 2)
      system.debug('==ThirdQuarter==');
else 
      system.debug('==fourthQuarter==');

Hope this helps!!!

Regards, Krishna

3
  • I guess that when you say month == 0, you mean i==0? That's why the variable should be named 'quarter' instead :)
    – mkorman
    Commented Feb 5, 2016 at 10:33
  • yes..you are correct @mkorman Commented Feb 8, 2016 at 6:37
  • 1
    Thats incorrect, For June , it is 6. How 6/3 = 2 is third quarter? Commented Sep 17, 2017 at 5:25
2

Use the Apex code below.

for (integer month = 1; month <= 12; month++) {
    system.debug(month + ' = ' + math.round((month+1)/3.0));
}
2
  • 3
    Magic numbers? Please care to explain.
    – Raul
    Commented Oct 16, 2017 at 14:45
  • There is no magic number here. 3.0 is number of months in a quarter. When you add 1 to the month number and divide it by 3.0 (to make the expression a double data type) and round it. You get the quarter number. Commented Oct 17, 2017 at 16:26

You must log in to answer this question.

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