This blog explains about getting Fiscal Month Start & End date , Fiscal Week start & end date based on given date using Java API
From the below diagram,
For October 2013, 9/29/2013 is Fiscal Month Start Date , 11/9/2013 is Fiscal Month End Date
Similarly - 9/29/2013 is fiscal week start date , 10/5/2013 is fiscal week end date for any date b/w 1st to 5th in October.
Implementation
From the below diagram,
For October 2013, 9/29/2013 is Fiscal Month Start Date , 11/9/2013 is Fiscal Month End Date
Similarly - 9/29/2013 is fiscal week start date , 10/5/2013 is fiscal week end date for any date b/w 1st to 5th in October.
Implementation
private static GregorianCalendar gregorianCalendar;
private static final int MAX_WEEKS = 6;
private static void init(Date date) {
gregorianCalendar = new GregorianCalendar();
gregorianCalendar.clear();
gregorianCalendar.setTime(date);
}
/**
* Returns the Start date of fiscal week based on given input Date
* @param date
* @return
* @throws Exception
*/
public static Date getFiscalWeekStartDate(Date date) throws Exception {
if (date != null) {
init(date);
return getFiscalWeekStartDate(gregorianCalendar);
}
return null;
}
private static Date getFiscalWeekStartDate(GregorianCalendar gregorianCalendar) {
int correction = 1 - gregorianCalendar.get(GregorianCalendar.DAY_OF_WEEK);
gregorianCalendar.add(Calendar.DATE, correction);
return gregorianCalendar.getTime();
}
/**
* Returns the Start date of fiscal week based on given input Date
* @param date
* @return
* @throws Exception
*/
public static Date getFiscalWeekEndDate(Date date) throws Exception {
if (date != null) {
init(date);
return getFiscalWeekEndDate(gregorianCalendar);
}
return null;
}
private static Date getFiscalWeekEndDate(GregorianCalendar gregorianCalendar) {
int correction = 7 - gregorianCalendar.get(GregorianCalendar.DAY_OF_WEEK);
gregorianCalendar.add(Calendar.DATE, correction);
return gregorianCalendar.getTime();
}
/**
* Returns the Start date of fiscal month based on given input Date
* @param date
* @return
* @throws Exception
*/
public static Date getFiscalMonthStartDate(Date date) throws Exception {
if (date != null) {
init(date);
int correction = 1 - gregorianCalendar.get(GregorianCalendar.DAY_OF_MONTH);
gregorianCalendar.add(Calendar.DATE, correction);
return getFiscalWeekStartDate(gregorianCalendar);
}
return null;
}
/**
* Returns the End date of fiscal month based on given input Date
* @param date
* @return
* @throws Exception
*/
public static Date getFiscalMonthEndDate(Date date) throws Exception {
if (date != null) {
int correction = 0;
init(date);
int num_of_weeks = gregorianCalendar.getActualMaximum(Calendar.WEEK_OF_MONTH);
int cur_week = gregorianCalendar.get(Calendar.WEEK_OF_MONTH);
if (num_of_weeks < MAX_WEEKS) {
correction = num_of_weeks - cur_week + 1;
} else {
correction = num_of_weeks - cur_week;
}
gregorianCalendar.add(Calendar.WEEK_OF_MONTH, correction);
return getFiscalWeekEndDate(gregorianCalendar);
}
return null;
}
No comments:
Post a Comment