This utility class was built to provide the same date literal values that Salesforce built for SOQL queries in generic Apex.
The class supports all date literals found in the old documentation (screnshots below), which includes all those in the new documentation as well as a few others. In addition, I've added support for half years, fiscal half years, & N [Period Type]S FROM NOW.
The boundaries returned by DateService are an inclusive representation of given date literals and can be used like so:
Map<String,DateTime> tomorrowBoundaryByDateTime = DateService.dateLiteralToDateTimeMap(DateLiteral.TOMORROW);
DateTime lastTwoMonthsStartBoundary = DateService.dateLiteralToStartDateTime(DateLiteral.LAST_N_MONTHS,2);
Date endSevenFiscalYearsBoundary = DateService.dateLiteralToEndDate(DateLiteral.NEXT_N_FISCAL_YEARS,7);
System.assertEquals( endSevenFiscalYearsBoundary, DateService.dateLiteralToDateMap(DateLiteral.NEXT_N_FISCAL_YEARS,7).get(DateService.END_KEY) );
Documentation was created using ApexDocs (originally developed by Aslam Bari and later improved by Steve Cox) and has been included in the project
Happy Coding!


