01: package com.jamonapi.utils;
02:
03: import java.util.*;
04:
05: /** Comparator that allows you to pass Calendar fields and a negative number for the number
06: * of this filed (i.e. hours/days) that a Date should not exceed. Use fields like Calendar.DATE, HOUR_OF_DAY, DAY_OF_MONTH, DAY_OF_WEEK, DAY_OF_YEAR ETC.
07: * . Values to b used for dateToAdd in the constructor could be -7 for 7 days ago, or -24 for 24 hours ago depending on what
08: * was passed in the dateField.
09: *
10: * @author steve souza
11: *
12: */
13: public class DateMathComparator extends JAMonComparator {
14: private int dateField;
15: private int dateToAdd;
16: /**Use fields like Calendar.DATE, HOUR_OF_DAY, DAY_OF_MONTH, DAY_OF_WEEK, DAY_OF_YEAR ETC.*/
17: private Calendar calendar = new GregorianCalendar();
18:
19: private static final boolean NATURAL_ORDER = true;
20:
21: public DateMathComparator(int dateField, int dateToAdd) {
22: super (NATURAL_ORDER);
23: this .dateField = dateField;
24: this .dateToAdd = dateToAdd;
25: }
26:
27: protected int compareThis(Object o1, Object o2) {
28: int retVal = 0;
29: if (o1 instanceof Date && o2 instanceof Date) {
30: Date d1 = (Date) o1;
31: Date d2 = (Date) o2;
32:
33: calendar.setTime(new Date());
34: calendar.add(dateField, dateToAdd);// i.e. todays date -7 days
35: Date dateAddValue = calendar.getTime();
36:
37: // 2 dates are equal if they are both above or below the Calendar field threshold
38: // else date1 is greater if it is greater than threshold and date2 isn't
39: // and date1 is less than the threshold than it is greater than date2.
40: int d1CompNum = d1.compareTo(dateAddValue);
41: int d2CompNum = d2.compareTo(dateAddValue);
42:
43: if (d1CompNum <= -1 && d2CompNum <= -1)
44: return 0;
45: else if (d1CompNum >= 1 && d2CompNum >= 1)
46: return 0;
47: else if (d1CompNum == 1)
48: return 1;
49: else if (d1CompNum == -1)
50: return -1;
51: }
52:
53: return retVal;
54:
55: }
56:
57: /** Test code */
58: public static void main(String[] arg) {
59: //7 DAYS
60: //24 HOURS
61: Date today = new Date();
62:
63: Calendar calendar = new GregorianCalendar();
64: calendar.setTime(today);
65: calendar.add(Calendar.DAY_OF_YEAR, -8);
66: Date minus8Days = calendar.getTime();
67: System.out.println(new DateMathComparator(Calendar.DAY_OF_YEAR,
68: -7).compare(today, minus8Days));
69:
70: calendar.setTime(today);
71: calendar.add(Calendar.DAY_OF_YEAR, -6);
72: Date minus6Days = calendar.getTime();
73: System.out.println(new DateMathComparator(Calendar.DAY_OF_YEAR,
74: -7).compare(today, minus6Days));
75:
76: calendar.setTime(today);
77: calendar.add(Calendar.HOUR_OF_DAY, -6);
78: Date minus6Hours = calendar.getTime();
79: System.out.println(new DateMathComparator(Calendar.HOUR_OF_DAY,
80: -7).compare(today, minus6Hours));
81:
82: calendar.setTime(today);
83: calendar.add(Calendar.HOUR_OF_DAY, -8);
84: Date minus8Hours = calendar.getTime();
85: System.out.println(new DateMathComparator(Calendar.HOUR_OF_DAY,
86: -7).compare(today, minus8Hours));
87:
88: }
89:
90: }
|