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
banu201banu201 

How to get minimum date from list of dates in apex class

Hi,

 

I need to find Minimum date and maximum date from list of dates.Can anyone give me solution?

 

Regards

banu

JimRaeJimRae

If the list is an actual List<Date> collection, you can use the sort method on it.

 

extract element 0 from the sorted list for the smallest or earliest date, and select the list size -1 for the largest or latest date.

 

Here is a little sample code to show you how it works:

 

List<Date> dlist = new List<Date>();
for(integer i=0;i<100;i++){
dlist.add(date.newinstance(2011,1,1).adddays(-i));
}
dlist.sort();
system.debug('\n\nsmallest date: '+dlist[0]);
system.debug('\n\nlargest date: '+dlist[dlist.size()-1]);