function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
shiv kumar 48shiv kumar 48 

Compare dates in salesforce

 Lets say i have four dates , how to find which one is the latest date using apex ?
or
 If we add dates to a List of dates can we sort ?
 
Chamil MadusankaChamil Madusanka
Dates are primitives in Apex and can be compared directly:
 
Date startDate = Date.newInstance(2008, 1, 1);
Date dueDate = Date.newInstance(2008, 1, 30);

if(startDate > dueDate)
{
}

If you get the answer, please mark it as the correct answer. It will be a help to others who are facing the same problem later.
 
shiv kumar 48shiv kumar 48
@CM
Thanks for your reply , it solves my problem upto some extend ,This approach actually having lot of if else .
I just found another approach which omits all if and else condition hope u like it :


List<Date> lisd = new List<Date>();
Date t1= Date.today();
Date t2 = Date.today() - 2;
Date t3 = Date.today() + 3;
lisd.add(t1);
lisd.add(t2);
lisd.add(t3);
lisd.sort();
System.debug('Latest Date'+ lisd[2]);