001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.activity;
018:
019: import java.util.Calendar;
020: import java.util.Date;
021:
022: import org.ofbiz.base.util.Debug;
023: import org.ofbiz.entity.GenericDelegator;
024:
025: import com.sourcetap.sfa.util.Preference;
026: import com.sourcetap.sfa.util.UserInfo;
027:
028: /**
029: * DOCUMENT ME!
030: *
031: */
032: public class CalendarUtil {
033:
034: public static final String module = CalendarUtil.class.getName();
035:
036: public static final String[] monthLabel = { "", "January",
037: "February", "March", "April", "May", "June", "July",
038: "August", "September", "October", "November", "December" };
039: public static final String[] dayLabel = { "Sunday", "Monday",
040: "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
041:
042: public static final int[] months = { Calendar.JANUARY,
043: Calendar.FEBRUARY, Calendar.MARCH, Calendar.APRIL,
044: Calendar.MAY, Calendar.JUNE, Calendar.JULY,
045: Calendar.AUGUST, Calendar.SEPTEMBER, Calendar.OCTOBER,
046: Calendar.NOVEMBER, Calendar.DECEMBER };
047:
048: public static final int[] daysInMonth = { 0, 31, 28, 31, 30, 31,
049: 30, 31, 31, 30, 31, 30, 31 };
050:
051: /**
052: * DOCUMENT ME!
053: *
054: * @return
055: */
056: public static String[] getMonthLabels() {
057: return monthLabel;
058: }
059:
060: /**
061: * DOCUMENT ME!
062: *
063: * @return
064: */
065: public static String[] getDayLabels() {
066: return dayLabel;
067: }
068:
069: /**
070: * DOCUMENT ME!
071: *
072: * @return
073: */
074: public static int[] getDaysInMonth() {
075: return daysInMonth;
076: }
077:
078: /**
079: * DOCUMENT ME!
080: *
081: * @param year
082: * @param month
083: * @param day
084: *
085: * @return
086: */
087: public static Calendar getCalendar(int year, int month, int day) {
088: Calendar calendar = Calendar.getInstance();
089:
090: if (year <= 0) {
091: year = calendar.get(Calendar.YEAR);
092: }
093:
094: if ((month <= 0) || (month > 12)) {
095: month = calendar.get(Calendar.MONTH) + 1;
096: }
097:
098: if (day <= 0) {
099: day = calendar.get(Calendar.DAY_OF_MONTH);
100: }
101:
102: calendar.clear();
103:
104: calendar.set(Calendar.MONTH, months[month]);
105: calendar.set(Calendar.YEAR, year);
106: calendar.set(Calendar.DAY_OF_MONTH, day);
107:
108: return calendar;
109: }
110:
111: public static int convertCalendarMonthToFiscalMonth(int calMonth,
112: int fiscalYearStartMonth) {
113: int month = (12 + calMonth - fiscalYearStartMonth + 1) % 12;
114: if (month == 0)
115: month = 12;
116: return month;
117: }
118:
119: public static int getQuarter(int calMonth, int fiscalYearStartMonth) {
120: int month = convertCalendarMonthToFiscalMonth(calMonth,
121: fiscalYearStartMonth);
122: int quarter = ((month - 1) / 3) + 1;
123: return quarter;
124: }
125:
126: public static String getQuarterString(int month) {
127: return getQuarterString(month, 1);
128: }
129:
130: /**
131: * DOCUMENT ME!
132: *
133: * @param month
134: *
135: * @return
136: */
137: public static String getQuarterString(int month,
138: int fiscalYearStartMonth) {
139: if (month <= 0) {
140: month = (Calendar.getInstance().get(Calendar.MONTH) + 1);
141: }
142:
143: String returnString = "";
144:
145: // handle fyOffset
146: int quarter = getQuarter(month, fiscalYearStartMonth);
147:
148: switch (quarter) {
149: case 1:
150: returnString = "Q1";
151: break;
152: case 2:
153: returnString = "Q2";
154: break;
155: case 3:
156: returnString = "Q3";
157: break;
158: case 4:
159: returnString = "Q4";
160: break;
161: default:
162: returnString = "Q?";
163: Debug.logError("invalid quarter:" + quarter
164: + " for m,fyM = " + month + ", "
165: + fiscalYearStartMonth, module);
166: break;
167: }
168:
169: return returnString;
170: }
171:
172: public static String[] getQuarterMonthNames(int month,
173: int fiscalYearStartMonth) {
174: return getQuarterMonthLabels(month, fiscalYearStartMonth);
175: }
176:
177: /**
178: * DOCUMENT ME!
179: *
180: * @param month
181: *
182: * @return
183: */
184: public static String[] getQuarterMonthLabels(int month,
185: int fiscalYearStartMonth) {
186: if (month <= 0) {
187: month = (Calendar.getInstance().get(Calendar.MONTH) + 1);
188: }
189:
190: int[] monthNumbers = getQuarterMonthNumbers(month,
191: fiscalYearStartMonth);
192:
193: String[] returnString = new String[3];
194:
195: for (int i = 0; i < 3; i++)
196: returnString[i] = monthLabel[monthNumbers[i]];
197:
198: return returnString;
199: }
200:
201: public static int[] getQuarterMonthNumbers(int month,
202: int fiscalYearStartMonth) {
203: return getQuarterNumbers(month, fiscalYearStartMonth);
204: }
205:
206: /**
207: * DOCUMENT ME!
208: *
209: * @param month
210: *
211: * @return
212: */
213: public static int[] getQuarterNumbers(int month,
214: int fiscalYearStartMonth) {
215: if (month <= 0) {
216: month = (Calendar.getInstance().get(Calendar.MONTH) + 1);
217: }
218:
219: int[] returnNumbers = new int[3];
220:
221: // handle fyOffset
222:
223: int quarter = getQuarter(month, fiscalYearStartMonth);
224: int firstMonth = (fiscalYearStartMonth + (quarter - 1) * 3) % 12;
225:
226: for (int i = 0; i < 3; i++) {
227: returnNumbers[i] = ((firstMonth + i) % 12);
228: if (returnNumbers[i] == 0)
229: returnNumbers[i] = 12;
230: }
231:
232: return returnNumbers;
233: }
234:
235: /**
236: * DOCUMENT ME!
237: *
238: * @param month
239: *
240: * @return
241: */
242: public static int[] getPreviousQuarterNumbers(int month,
243: int fiscalYearStartMonth) {
244:
245: int[] currentNumbers = getQuarterNumbers(month,
246: fiscalYearStartMonth);
247:
248: int[] returnNumbers = new int[3];
249:
250: for (int i = 0; i < 3; i++) {
251: returnNumbers[i] = (12 + currentNumbers[i] - 3) % 12;
252: if (returnNumbers[i] == 0)
253: returnNumbers[i] = 12;
254: }
255:
256: return returnNumbers;
257: }
258:
259: /**
260: * DOCUMENT ME!
261: *
262: * @param month
263: *
264: * @return
265: */
266: public static int[] getNextQuarterNumbers(int month,
267: int fiscalYearStartMonth) {
268:
269: int[] currentNumbers = getQuarterNumbers(month,
270: fiscalYearStartMonth);
271:
272: int[] returnNumbers = new int[3];
273:
274: for (int i = 0; i < 3; i++) {
275: returnNumbers[i] = (currentNumbers[i] + 3) % 12;
276: if (returnNumbers[i] == 0)
277: returnNumbers[i] = 12;
278: }
279:
280: return returnNumbers;
281: }
282:
283: /**
284: * DOCUMENT ME!
285: *
286: * @param month
287: * @param year
288: *
289: * @return
290: */
291: public static Date getMinimumQuarterDate(int month, int year,
292: int fiscalYearStartMonth) {
293: if (month <= 0) {
294: month = (Calendar.getInstance().get(Calendar.MONTH) + 1);
295: }
296:
297: if (year <= 0) {
298: year = (Calendar.getInstance().get(Calendar.YEAR));
299: }
300:
301: int monthNumbers[] = getQuarterMonthNumbers(month,
302: fiscalYearStartMonth);
303: int firstMonth = monthNumbers[0];
304:
305: Calendar calendar = Calendar.getInstance();
306: if (firstMonth < fiscalYearStartMonth)
307: year++;
308:
309: calendar.set(Calendar.YEAR, year);
310: calendar.set(Calendar.MONTH, firstMonth - 1);
311: calendar.set(Calendar.DAY_OF_MONTH, 1);
312:
313: return calendar.getTime();
314: }
315:
316: /**
317: * DOCUMENT ME!
318: *
319: * @param month
320: * @param year
321: *
322: * @return
323: */
324: public static Date getMaximumQuarterDate(int month, int year,
325: int fiscalYearStartMonth) {
326: if (month <= 0) {
327: month = (Calendar.getInstance().get(Calendar.MONTH) + 1);
328: }
329:
330: if (year <= 0) {
331: year = (Calendar.getInstance().get(Calendar.YEAR));
332: }
333:
334: int monthNumbers[] = getQuarterMonthNumbers(month,
335: fiscalYearStartMonth);
336: int lastMonth = monthNumbers[2] + 1;
337: if (lastMonth < 12)
338: lastMonth = lastMonth % 12;
339:
340: Calendar calendar = Calendar.getInstance();
341:
342: if (lastMonth <= fiscalYearStartMonth)
343: year++;
344: calendar.set(Calendar.YEAR, year);
345: calendar.set(Calendar.MONTH, lastMonth - 1);
346: calendar.set(Calendar.DAY_OF_MONTH, 1);
347: calendar.add(Calendar.DATE, -1);
348:
349: return calendar.getTime();
350: }
351:
352: /**
353: * DOCUMENT ME!
354: *
355: * @param startDate
356: * @param endDate
357: * @param calendarReturnField
358: *
359: * @return
360: */
361: public static int differenceBetweenDates(Calendar startDate,
362: Calendar endDate, int calendarReturnField) {
363: int returnInt = -1;
364:
365: if (startDate.before(endDate)) {
366: switch (calendarReturnField) {
367:
368: case Calendar.MONTH:
369:
370: if (startDate.get(Calendar.YEAR) == endDate
371: .get(Calendar.YEAR)) {
372: //same year
373: returnInt = (endDate.get(Calendar.MONTH
374: - startDate.get(Calendar.MONTH)));
375: } else {
376: //cross years
377: returnInt = endDate.get(Calendar.MONTH) + 1;
378:
379: if ((endDate.get(Calendar.YEAR) - startDate
380: .get(Calendar.YEAR)) > 1) {
381: int numYrs = (endDate.get(Calendar.YEAR) - startDate
382: .get(Calendar.YEAR));
383: returnInt = returnInt + (12 * numYrs);
384: }
385:
386: returnInt = returnInt
387: + (12 - (startDate.get(Calendar.MONTH) + 1));
388: }
389:
390: break;
391:
392: case Calendar.DAY_OF_MONTH:
393:
394: if (startDate.get(Calendar.YEAR) == endDate
395: .get(Calendar.YEAR)) {
396: if (startDate.get(Calendar.MONTH) == endDate
397: .get(Calendar.MONTH)) {
398: //same year, same month
399: returnInt = (endDate.get(Calendar.DAY_OF_MONTH) - startDate
400: .get(Calendar.DAY_OF_MONTH));
401: } else {
402: //same year, cross months
403: returnInt = (endDate.get(Calendar.DAY_OF_YEAR) - startDate
404: .get(Calendar.DAY_OF_YEAR));
405: }
406: } else {
407: //cross years, cross months
408: //get number of days between years
409: //get number
410: //TODO: Finish this part of the method
411: }
412:
413: break;
414:
415: case Calendar.DAY_OF_YEAR:
416:
417: if (startDate.get(Calendar.YEAR) == endDate
418: .get(Calendar.YEAR)) {
419: returnInt = endDate.get(Calendar.DAY_OF_YEAR)
420: - startDate.get(Calendar.DAY_OF_YEAR);
421: } else {
422: returnInt = endDate.get(Calendar.DAY_OF_YEAR);
423:
424: if ((endDate.get(Calendar.YEAR) - startDate
425: .get(Calendar.YEAR)) > 1) {
426: int numYrs = (endDate.get(Calendar.YEAR) - startDate
427: .get(Calendar.YEAR));
428: returnInt = returnInt + (365 * numYrs);
429: }
430:
431: returnInt = returnInt
432: + (365 - (startDate
433: .get(Calendar.DAY_OF_YEAR)));
434: }
435:
436: break;
437:
438: case Calendar.YEAR:
439: returnInt = (endDate.get(Calendar.YEAR) - startDate
440: .get(Calendar.YEAR));
441: break;
442:
443: default:
444: break;
445:
446: }
447: }
448:
449: return returnInt;
450: }
451:
452: public static int getFiscalYearStartMonth(
453: GenericDelegator delegator, UserInfo userInfo) {
454: Preference pref = Preference.getInstance(delegator);
455:
456: int FYStartMonth = pref.getPreference(userInfo.getPartyId(),
457: userInfo.getAccountId(), "FISCAL_YEAR_START", 1);
458:
459: return FYStartMonth;
460: }
461:
462: }
|