Let’s say our business hours are from 9am to 6pm (Monday to Friday) and if Lead is created at 5pm on Monday an email alert should be sent to LeadOwner at 12am on Tuesday.
You can set your business hours in Salesforce under Company Profile.
Above requirement can be achieved by using BusinessHours function in apex.
BusinessHours.add(businessHoursId, startDate, intervalMilliseconds)
Datetime currentTime = System.now(); Id businessHourId = [SELECT Id FROM BusinessHours WHERE IsDefault = true].id; Integer hours =4 ; Datetime emailSendDate = BusinessHours.add(businessHourId, currentTime, hours* 60 * 60 * 1000L);