1:
31: package ;
32:
33: import ;
34: import ;
35: import ;
36: import ;
37: import ;
38:
39: import ;
40: import ;
41: import ;
42: import ;
43:
44:
49: public class DateUtil
50: {
51: private static final Date ISO8001_TIME = new GregorianCalendar().getTime();
52:
53:
65: public static Date normalizeDate(Date fromDate, Type toType)
66: {
67: return normalizeDate(fromDate, toType, true);
68: }
69:
70: public static Date normalizeDate(Date fromDate, Type toType,
71: boolean convertSerial)
72: {
73: if (fromDate == null || toType == null)
74: {
75: throw new IllegalArgumentException();
76: }
77:
78: if (convertSerial)
79: {
80: Number serial = toSerialDate(fromDate, null);
81: serial = normalizeDate(serial, toType);
82: fromDate = toJavaDate(serial, null);
83: }
84:
85:
86:
87:
88: if (toType.isFlagSet(Type.TIME_TYPE))
89: {
90: return new Time(fromDate.getTime());
91: }
92: else if (toType.isFlagSet(Type.DATE_TYPE))
93: {
94: return new java.sql.Date(fromDate.getTime());
95: }
96: else if (toType.isFlagSet(Type.DATETIME_TYPE))
97: {
98: return new Date(fromDate.getTime());
99: }
100:
101: return fromDate;
102: }
103:
104: public static Number normalizeDate(Number fromSerialDate, Type toType)
105: {
106: if (fromSerialDate == null || toType == null)
107: {
108: throw new IllegalArgumentException();
109: }
110:
111: final BigDecimal o = new BigDecimal(fromSerialDate.doubleValue()).setScale(
112: 5, BigDecimal.ROUND_UP);
113:
114: if (toType.isFlagSet(Type.TIME_TYPE))
115: {
116: return o.subtract(new BigDecimal(o.intValue()));
117:
118:
119:
120:
121: }
122: else if (toType.isFlagSet(Type.DATE_TYPE))
123: {
124: return new Integer(fromSerialDate.intValue());
125: }
126:
127: else
128: {
129: return o;
130: }
131: }
132:
133: public static Date toJavaDate(Number serialDate, LocalizationContext context)
134: {
135: final Date javaDate = HSSFDateUtil.getJavaDate(serialDate.doubleValue());
136:
137: final long l = (javaDate.getTime() / 1000) * 1000;
138:
139:
140:
141:
142: return new Date(l);
143: }
144:
145: public static Number toSerialDate(Date date, LocalizationContext context)
146: {
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157: final double serial = HSSFDateUtil.getExcelDate(date);
158: return new Double(serial);
159: }
160:
161: public static void main(String[] args)
162: {
163: final DefaultLocalizationContext context = new DefaultLocalizationContext();
164: final java.sql.Date createDate = createDate(2006, 05, 01, context);
165: final Number serial = toSerialDate(createDate, context);
166: System.out.println(createDate);
167: System.out.println(serial);
168: final Date toJavaDate = toJavaDate(serial, context);
169: System.out.println(normalizeDate(toJavaDate, DateTimeType.DATE_TYPE));
170: System.out.println(toJavaDate);
171: System.out.println(HSSFDateUtil.getJavaDate(serial.doubleValue()));
172: }
173:
174: public static Date now(LocalizationContext context)
175: {
176: final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
177: context.getLocale());
178: gc.set(Calendar.MILLISECOND, 0);
179:
180: return gc.getTime();
181: }
182:
183: public static Date createDateTime(int year, int month, int day, int hour,
184: int minute, int second, LocalizationContext context)
185: {
186: final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
187: context.getLocale());
188: gc.set(Calendar.DAY_OF_MONTH, day);
189: gc.set(Calendar.MONTH, month);
190: gc.set(Calendar.YEAR, year);
191: gc.set(Calendar.MILLISECOND, 0);
192: gc.set(Calendar.HOUR_OF_DAY, hour);
193: gc.set(Calendar.MINUTE, minute);
194: gc.set(Calendar.SECOND, second);
195: return gc.getTime();
196: }
197:
198: public static Time createTime(int hour, int minute, int second,
199: LocalizationContext context)
200: {
201: final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
202: context.getLocale());
203: gc.setTime(ISO8001_TIME);
204: gc.set(Calendar.MILLISECOND, 0);
205: gc.set(Calendar.HOUR_OF_DAY, hour);
206: gc.set(Calendar.MINUTE, minute);
207: gc.set(Calendar.SECOND, second);
208: return new Time(gc.getTime().getTime());
209: }
210:
211: public static java.sql.Date createDate(int year, int month, int day,
212: LocalizationContext context)
213: {
214: final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
215: context.getLocale());
216: gc.set(Calendar.DAY_OF_MONTH, day);
217: gc.set(Calendar.MONTH, month - 1);
218: gc.set(Calendar.YEAR, year);
219: gc.set(Calendar.MILLISECOND, 0);
220: gc.set(Calendar.HOUR_OF_DAY, 0);
221: gc.set(Calendar.MINUTE, 0);
222: gc.set(Calendar.SECOND, 0);
223: return new java.sql.Date(gc.getTime().getTime());
224: }
225:
226: public static Calendar createCalendar(Date date, LocalizationContext context)
227: {
228: final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
229: context.getLocale());
230: gc.setTime(date);
231: return gc;
232: }
233:
234: }