When working with Salesforce data, it's often necessary to filter or report on records from a specific quarter . If you're looking to retrieve the start and end dates of the previous quarter based on the current date, here’s a simple and reusable Apex utility you can use. 🔧 Apex Utility Methods Below are two helper methods: getStartDate() — Calculates the start date of the previous quarter . getEndDate() — Calculates the end date of the previous quarter , based on the start date. // Helper method to get the start date of the previous quarter private Date getStartDate () { Date today = Date . today (); Integer currentYear = today . year (); Integer currentMonth = today . month (); // Determine the first month of the previous quarter Integer previousQuarterStartMonth = (( currentMonth - 1 ) / 3 ) * 3 + 1 - 3 ; // Adjust the year if the previous quarter falls in the previ...
How to Calculate the Day of the Week from a Date and Actual Time Spent Between Two Date-Times in Apex
When working with Salesforce, there are instances where we need to compute the day of the week from a given date or determine the actual time spent between two date-time values, considering only business hours. For example, if we need to track the exact amount of time a user spent on a Lead, Case, or Task, simply subtracting two date-time values won't suffice if we need to exclude non-working hours. In this post, we will cover: How to determine the day of the week from a given date in Apex. How to calculate actual time spent between two date-times, while considering business hours (Monday to Friday: 8 AM - 8 PM, Saturday & Sunday: 8 AM - 4:30 PM). 1. How to Determine the Day of the Week from a Date in Apex Salesforce does not provide a direct method to get the day name (Monday, Tuesday, etc.) from a Date field. However, we can achieve this using toStartOfWeek() and daysBetween() methods . public static String getDayOfWeek ( Date inputDate ) { // Get the start...