As we have scenario to fetch greater date value from the multiple objects like I have to find greatest createddate from 2-3 contacts data so that this date can be used further.
Lets consider the scenario on contact objects.
List<DateTime> listCreatedDates = new List<DateTime>();
List<Contact> cons = [select id, CreatedDate from Contact limit 2];
For(Contact c : cons) {
listCreatedDates.add(c.CreatedDate);
}
listCreatedDates.sort();
DateTime minDate = listCreatedDates.get(0);
DateTime maxDate = listCreatedDates.get(listCreatedDates.size()-1);
System.debug('minDate : ' + minDate );
System.debug('maxDate : ' + maxDate );
Lets discuss about the above code:
Output for me -
0 comments:
Post a Comment
If you have any doubts, please let me know.