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!
How 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 July 2026
Contest runs July 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.