Working on a component in which fromdate and toDate are added inside salesforce. enter image description here

Criteria is something like any new from/end date should not be a part of precious record present in List. Tried to add code like :

if((new Date(userStartDate) >= new Date(StartDateVal)) && (new Date(userStartDate)<= new Date(EndDateVal)))
            {add error
else if((new Date(endDate) >= new Date(StartDateVal)) && (new Date(endDate)<=new Date(EndDateVal)))
            { add error

Is there any better way of comparing including usage of Maps etc


For small-ish lists, you can use the DateRange class.

public class DateRange {
    DateTime begins, ends;
    public DateRange(DateTime b, DateTime e) {
        begins = b;
        ends = e;
    public Integer hashCode() {
        return 1;
    public Boolean equals(Object o) {
        DateRange r = (DateRange)o;
        return begins < r.ends && ends > r.begins;

To use it:

Set<DateRange> ranges = new Set<DateRange>();
for(Integer index = 0, size = inputs.size(); index < size; index++) {
  DateRange key = new DateRange(startDates[index], endDates[index]);
  if(ranges.contains(key)) {
    // record an error for this index;
  } else {

Note that because of how this class operates, if you add a range when contains returns true, it will screw up the internal state of the Set/Map. However, it will perform decently well on lists of up to a few hundred items.

This algorithm is an exponential-growth algorithm, specifically average is n2/2 executions, and max executions is n2. I've tested this up to about 1,000 items without a problem, though.

It's much faster than the manual:

for(Integer i = 0, s = items.size(); i < s; i++) {
  for(Integer j = i + 1; j < s; j++) {

If you can, do this validation in JavaScript, it's much more efficient.

| improve this answer | |
  • 2
    So you can define variables or properties with coma! Never seen or used that in apex so far! – kurunve Jun 2 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.