01: package org.claros.commons.comparators;
02:
03: import java.util.Comparator;
04: import java.util.Date;
05:
06: /**
07: * @author Umut Gokbayrak
08: */
09: public class DateComparator implements Comparator<Date> {
10: private int left = 1;
11: private int right = -1;
12:
13: public DateComparator(boolean ascending) {
14: if (ascending) {
15: left = -1;
16: right = 1;
17: }
18: }
19:
20: /* (non-Javadoc)
21: * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
22: */
23: public int compare(Date arg0, Date arg1) {
24: if (arg0 != null && arg1 != null && arg0 instanceof Date
25: && arg1 instanceof Date) {
26: Date dt1 = (Date) arg0;
27: Date dt2 = (Date) arg1;
28:
29: if (dt1.after(dt2)) {
30: return right;
31: } else if (dt1.before(dt2)) {
32: return left;
33: } else {
34: return 0;
35: }
36: }
37: return right;
38: }
39: }
|