You need to sign in to do that
Don't have an account?
Rahul 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.
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.
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.
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
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!!!
Thanks,
Maharajan.C
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?
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?