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