01: package com.jidesoft.grouper.date;
02:
03: import com.jidesoft.converter.ConverterContext;
04: import com.jidesoft.grouper.AbstractObjectGrouper;
05:
06: import java.util.Calendar;
07: import java.util.Date;
08:
09: /**
10: * An abstract Grouper which can take data type such as Date, Calendar or Long and provide {@link #getCalendarFieldAsInt(Object,int)}
11: * and {@link #getCalendarField(Object,int)} methods to access the field of the Calendar.
12: */
13: abstract public class DateGrouper extends AbstractObjectGrouper {
14: protected static Calendar INSTANCE = Calendar.getInstance();
15:
16: public static Object getCalendarField(Object value, int field) {
17: if (value instanceof Date) {
18: INSTANCE.setTime(((Date) value));
19: return INSTANCE.get(field);
20: } else if (value instanceof Long) {
21: INSTANCE.setTime(new Date((Long) value));
22: return INSTANCE.get(field);
23: } else if (value instanceof Calendar) {
24: return ((Calendar) value).get(field);
25: } else if (value == null) {
26: return null;
27: } else {
28: throw new IllegalArgumentException("Type incompatible");
29: }
30: }
31:
32: public static int getCalendarFieldAsInt(Object value, int field) {
33: if (value instanceof Date) {
34: INSTANCE.setTime(((Date) value));
35: return INSTANCE.get(field);
36: } else if (value instanceof Long) {
37: INSTANCE.setTime(new Date((Long) value));
38: return INSTANCE.get(field);
39: } else if (value instanceof Calendar) {
40: return ((Calendar) value).get(field);
41: } else if (value == null) {
42: return -1;
43: } else {
44: throw new IllegalArgumentException("Type incompatible");
45: }
46: }
47:
48: public Class<?> getType() {
49: return int.class;
50: }
51:
52: @Override
53: public ConverterContext getConverterContext() {
54: return null;
55: }
56: }
|