+ Start a Discussion
Rahul Singh 1384Rahul Singh 1384 

Check if any Date is fourth Friday of a month in APEX.

Hi all,

Can we Check if any Date is fourth Friday of a month in APEX?
Suppose we have a Date '25 Jan 2019'. how can we check if this is fourth friday of month or not in apex?

Any help would be highly appreciated.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Rahul,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
Date firstFriday = Date.today().toStartOfMonth().toStartOfWeek().addDays(5);   
System.debug('firstFriday 1 -> ' + firstFriday);

//add a week if "firstFriday" is in last month    
if(firstFriday < date.today().toStartOfmonth()) 
    firstFriday = firstFriday.addDays(7);
System.debug('firstFriday 2 -> ' + firstFriday);

Date fourthFriday = firstFriday.addDays(21);
System.debug('fourthFriday -> ' + fourthFriday);

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Maharajan CMaharajan C
Hi Rahul,

You can use the below method:

Apex Method : 
public Boolean weekOfMonth(Date todaysDate){
        Boolean isfourthFriday = false;
        Integer weekCount = 0;
        Integer startWeekResidue = 0;
        Integer endWeekResidue = 0;
        Datetime dt1 = DateTime.newInstance(todaysDate, Time.newInstance(0, 0, 0, 0));
        String dateStr = dt1 .format('EEEE');
        //Calculating startWeekResidue
        Date dt = todaysDate.toStartOfMonth().addDays(-1);
        Date dtFirstWeekend = dt.toStartOfWeek().addDays(6);
        startWeekResidue = dt.daysBetween(dtFirstWeekend);
        //Calculating endWeekResidue
        Date dtLastWeekend = todaysDate.toStartOfWeek().addDays(-1);
        endWeekResidue = dtLastWeekend.daysBetween(todaysDate);
        //Counting the weeks
        weekCount = (todaysDate.day() - (startWeekResidue + endWeekResidue))/7;
        weekCount += (startWeekResidue > 0 ? 1:0)+(endWeekResidue > 0 ? 1:0);
        System.Debug('Week Number: ' + weekCount);
        if(weekCount == 4  && dateStr == 'Friday') 
            system.debug( '@@@ The date Falls in 4th Week ==> ' + weekCount);
            isfourthFriday = True;

        return isfourthFriday;

To test the above use the below execution code:
Date myDate = date.newinstance(2019, 1, 25);
Boolean isfourthFriday = weekOfMonth(myDate );
System.debug('@@@ weeknum = ' + isfourthFriday );

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!

Rahul Singh 1384Rahul Singh 1384
Hi #Anas
First of all Thanks for the response.

It is working for the mentioned scenario, but struggling with this.
like I am trying to find find 'Fourth' 'Thursday' of Date[21/Feb/2019],
i am getting third thursday which is wrong. 
then how can i modify above Code.

Can you please help in this?
Rahul Singh 1384Rahul Singh 1384
Hi #Maharajan
Thanks for the response.

Here I am not getting correct result for this date[21June/2019] from the method you provided.
It is returning true, but it is not fourth friday, it is the third friday.

can you please check?