Given a starting date and a target weekday name (e.g., 'Monday', 'Tuesday'), write a method to find the next occurrence of that weekday after the given date. If the starting date itself is the target weekday, return the NEXT occurrence (7 days later), not the same date.
Method Signature:
public static Date getNextWeekday(Date startDate, String weekdayName)
Examples:
Example 1:
- Input:
Date startDate = Date.newInstance(2026, 3, 10)(Tuesday),weekdayName = 'Friday' - Output:
Date.newInstance(2026, 3, 13)(Friday, March 13, 2026) - Explanation: Starting from Tuesday March 10, the next Friday is March 13, which is 3 days later.
Example 2:
- Input:
Date startDate = Date.newInstance(2026, 3, 10)(Tuesday),weekdayName = 'Tuesday' - Output:
Date.newInstance(2026, 3, 17)(Tuesday, March 17, 2026) - Explanation: Since the start date is already Tuesday, return the NEXT Tuesday (7 days later).
Example 3:
- Input:
startDate = null,weekdayName = 'Monday' - Output:
null - Explanation: Returns null for null input.
Example 4:
- Input:
Date startDate = Date.newInstance(2026, 3, 13)(Friday),weekdayName = 'Monday' - Output:
Date.newInstance(2026, 3, 16)(Monday, March 16, 2026) - Explanation: Starting from Friday March 13, the next Monday is March 16, which is 3 days later.
Apex Code Editor
Welcome to Lightning Challenge!
Create an Account
Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.
Create accountHow It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay Mar 2026
Contest runs March 3 - 31. Complete challenges to climb the leaderboard!
Points are calculated for challenges completed during the contest period.