Get Start and End Dates of the Previous Quarter in Apex

A compact Apex utility to calculate the start and end Date of the previous quarter based on Date.today(). Includes code, explanation, and best practices for Salesforce developers.

Overview

When building reports, filters, or scheduled jobs in Salesforce, it’s common to need the date range for the previous fiscal or calendar quarter. The following Apex helper methods compute the first and last day of the previous quarter relative to the current date. They are simple, reusable, and handle year boundaries (e.g., previous quarter is in the prior year).

Helper methods (Apex)

Use these private helper methods inside a utility class or directly within your Apex logic:

// 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 previous year
    if (previousQuarterStartMonth <= 0) {
        previousQuarterStartMonth += 12;
        currentYear--;
    }
    // Return the first day of the previous quarter
    return Date.newInstance(currentYear, previousQuarterStartMonth, 1);
}

// Helper method to get the end date of the previous quarter
private Date getEndDate() {
    Date previousQuarterStartDate = getStartDate();
    // Add 3 months and subtract 1 day to get the last day of the quarter
    return previousQuarterStartDate.addMonths(3).addDays(-1);
}

How it works

The calculation first determines the start month of the current quarter using the expression:

((currentMonth - 1) / 3) * 3 + 1

Subtracting 3 from that result moves to the previous quarter. If the resulting start month is zero or negative, we add 12 and decrement the year to account for Q1 → previous quarter in the previous year.

Example

If today is April 25, 2025:

  • getStartDate() returns 2025-01-01
  • getEndDate() returns 2025-03-31

Best practices & variations

  • If your org follows a fiscal year that doesn't align with the calendar year, adjust the month math accordingly or pass a fiscal start offset into the helper.
  • Wrap these methods in a utility class and mark them as public static if you need to reuse them across classes or scheduled jobs.
  • For bulk processing or SOQL queries, use the returned Date values to filter queries (e.g., WHERE CreatedDate >= :startDate AND CreatedDate <= :endDate).

Conclusion

These two simple methods reliably return the previous quarter's date range and can be dropped into any Apex utility or job. They save time when creating reports, batch jobs, or time-based filters and handle year boundaries cleanly.

Why this matters: Salesforce admins, developers, and business users frequently build time-based analytics, scheduled processes, and reports. Having a reliable, tested utility for previous-quarter date ranges ensures consistent results across automation and reporting.