to Find the difference between 2 dates , you can compare the values. covert dates into Value(long) and than compare rather than using Arithmetic operation like subtraction etc.
public class CompareObjects implements Comparator {
@Override
public int compare(classA c1, classA c2) {
long value1 = c1.getDate().getTime();
long value2 = c2.getDate().getTime();
if (value2 > value1) {
return 1;
} else if (value1 > value2) {
return -1;
} else {
return 0;
}
}
public static void main(String[] args) {
classA o1 = new classA();
o1.setDate(new Date());
classA o2 = new classA();
o2.setDate(new Date());
CompareObjects compare = new CompareObjects();
int i = compare.compare(o1, o2);
System.out.println(" Result : " + i);
}
}
or without converting you can directly return the result.
return c2.getDate().compareTo(c1.getDate());
after comparison you can use Collection.sort method to set the order.