此代码允许您在给定的假设下设置周年日期并在不同的构造函数上设置会计年度。
public class FiscalYear {
public String fyAnnualStartDate = "02-01";
public Calendar fyCalStart;
public Calendar fyCalEnd;
public Date fyStartDate;
public Date fyEndDate;
public int year;
public int month;
public int day;
public String timeZone = "PST";
protected SimpleDateFormat isoSDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
public Date statusTime = null;
public String stringTime = "";
public String fyName;
/**
* This is our lazy constructor assumes this is 02-01 for the start year and PST for the time zone
*
* Assumes that the year passed is the start of the year which assigns to the fiscal year
*
* I.E. 2020 would be fy21
*
* @param year
* @throws ParseException
*/
public FiscalYear(int year) throws ParseException {
// TODO Auto-generated constructor stub
this.year = year;
setYearMonth();
setDates();
setName();
}
public FiscalYear(int year, String zone) throws ParseException {
// TODO Auto-generated constructor stub
this.year = year;
this.timeZone = zone;
setYearMonth();
setDates();
setName();
}
/**
* This constructor allows you to set the annualstartdate if you need to adjust the fiscal year to a new company
* @param year
* @param zone
* @param annualStartDate
* @throws ParseException
*/
public FiscalYear(int year, String zone, String annualStartDate) throws ParseException {
// TODO Auto-generated constructor stub
this.fyAnnualStartDate = annualStartDate;
this.year = year;
this.timeZone = zone;
setYearMonth();
setDates();
setName();
}
/**
* This constructor allows you to set the annualstartdate if you need to adjust the fiscal year to a new company
* However this allows you to provide the current date or date in general to confirm the true fiscal year
* @param year
* @param zone
* @param annualStartDate
* @throws Exception
*/
public FiscalYear(Date year, String zone, String annualStartDate) throws Exception {
// TODO Auto-generated constructor stub
this.timeZone = zone;
this.fyAnnualStartDate = annualStartDate;
this.year = getActualYear(year);
setYearMonth();
setDates();
setName();
}
/**
* This constructor allows you to provide the current date or date in general to confirm the true fiscal year
* Default annual start date is set in var fyAnnualStartDate
*
* @param year
* @throws Exception
*/
public FiscalYear(Date year) throws Exception {
// TODO Auto-generated constructor stub
this.year = getActualYear(year);
setYearMonth();
setDates();
setName();
}
/**
* Gets the actual FY to be correct given the window can a Fiscal year can have two years within it
*
* This tests to figure out which FY this should instantiate to given the date you are providing based on the start date and logic that
* Fiscal Year 21 corresponds to the a lesser actual start year
* I.E. 2020 would be fy21
* @param year2
* @return
* @throws Exception
*/
public static int getActualYear(Date year2) throws Exception {
// TODO Auto-generated method stub
SimpleDateFormat isoSDFTest = new SimpleDateFormat("yyyy");
FiscalYear test = new FiscalYear(Integer.parseInt(isoSDFTest.format(year2)));
FiscalYear testBefore = new FiscalYear(Integer.parseInt(isoSDFTest.format(year2))-1);
FiscalYear testAfter = new FiscalYear(Integer.parseInt(isoSDFTest.format(year2))+1);
if ((test.getStartDateAsDate().before(year2) && test.getEndDateAsDate().after(year2)) || year2.equals(test.getStartDateAsDate()) || year2.equals(test.getEndDateAsDate())) {
return Integer.parseInt(isoSDFTest.format(test.getStartDateAsDate()));
}
if ((testBefore.getStartDateAsDate().before(year2) && testBefore.getEndDateAsDate().after(year2)) || year2.equals(testBefore.getStartDateAsDate()) || year2.equals(testBefore.getEndDateAsDate())) {
return Integer.parseInt(isoSDFTest.format(testBefore.getStartDateAsDate()));
}
if ((testAfter.getStartDateAsDate().before(year2) && testAfter.getEndDateAsDate().after(year2)) || year2.equals(testAfter.getStartDateAsDate()) || year2.equals(testAfter.getEndDateAsDate())) {
return Integer.parseInt(isoSDFTest.format(testAfter.getStartDateAsDate()));
}
throw new Exception("Issue figuring window out");
}
private void setName() {
// TODO Auto-generated method stub
String fy = "FY";
this.fyName = fy+String.valueOf(year+1).substring(2,4);
}
private void setYearMonth() {
// TODO Auto-generated method stub
String [] arr = fyAnnualStartDate.split("-");
month = Integer.parseInt(arr[0])-1;
day = Integer.parseInt(arr[1]);
}
private void setDates() throws ParseException {
// TODO Auto-generated method stub
isoSDF.setTimeZone(TimeZone.getTimeZone(timeZone));
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone(timeZone));
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR_OF_DAY,0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);
fyCalStart = cal;
fyStartDate = cal.getTime();
Calendar endCal = (Calendar) cal.clone();
endCal.add(Calendar.YEAR, 1);
endCal.add(Calendar.SECOND,-1);
fyCalEnd = endCal;
fyEndDate = endCal.getTime();
statusTime = isoSDF.parse(isoSDF.format(new Date()));
stringTime = isoSDF.format(statusTime);
}
public String getStartDate() {
return isoSDF.format(fyStartDate);
}
public String getEndDate() {
return isoSDF.format(fyEndDate);
}
public Date getStartDateAsDate() {
return fyStartDate;
}
public Date getEndDateAsDate() {
return fyEndDate;
}
public void shiftWindow(int shift) {
fyCalStart.add(Calendar.YEAR, shift);
fyCalEnd.add(Calendar.YEAR, shift);
fyStartDate = fyCalStart.getTime();
fyEndDate = fyCalEnd.getTime();
year = year + shift;
setName();
}
public String getQuarterStartDate(int quarter) throws Exception {
Calendar temp = (Calendar) fyCalStart.clone();
quarter = validateQuarter(quarter);
if (quarter==1) {
} else if (quarter==2) {
temp.add(Calendar.MONTH, 3);
} else if (quarter==3) {
temp.add(Calendar.MONTH, 6);
} else {
temp.add(Calendar.MONTH, 9);
}
Date tempDate = temp.getTime();
return isoSDF.format(tempDate);
}
public String getQuarterEndDate(int quarter) throws Exception {
Calendar temp = (Calendar) fyCalEnd.clone();
quarter = validateQuarter(quarter);
if (quarter==1) {
temp.add(Calendar.MONTH, -9);
} else if (quarter==2) {
temp.add(Calendar.MONTH, -6);
} else if (quarter==3) {
temp.add(Calendar.MONTH, -3);
} else {
}
Date tempDate = temp.getTime();
return isoSDF.format(tempDate);
}
public String getMonthStartDate(int quarter, int month) throws Exception {
Calendar temp = (Calendar) fyCalStart.clone();
quarter = validateQuarter(quarter);
month = validateMonth(month);
if (quarter==1) {
if (month==1) {
} else if (month==2) {
temp.add(Calendar.MONTH, 1);
} else {
temp.add(Calendar.MONTH, 2);
}
} else if (quarter==2) {
if (month==1) {
temp.add(Calendar.MONTH, 3);
} else if (month==2) {
temp.add(Calendar.MONTH, 4);
} else {
temp.add(Calendar.MONTH, 5);
}
} else if (quarter==3) {
if (month==1) {
temp.add(Calendar.MONTH, 6);
} else if (month==2) {
temp.add(Calendar.MONTH, 7);
} else {
temp.add(Calendar.MONTH, 8);
}
} else {
if (month==1) {
temp.add(Calendar.MONTH, 9);
} else if (month==2) {
temp.add(Calendar.MONTH, 10);
} else {
temp.add(Calendar.MONTH, 11);
}
}
Date tempDate = temp.getTime();
return isoSDF.format(tempDate);
}
public String getMonthEndDate(int quarter, int month) throws Exception {
Calendar temp = (Calendar) fyCalEnd.clone();
quarter = validateQuarter(quarter);
month = validateMonth(month);
if (quarter==1) {
if (month==1) {
temp.add(Calendar.MONTH, -11);
} else if (month==2) {
temp.add(Calendar.MONTH, -10);
} else {
temp.add(Calendar.MONTH, -9);
}
} else if (quarter==2) {
if (month==1) {
temp.add(Calendar.MONTH, -8);
} else if (month==2) {
temp.add(Calendar.MONTH, -7);
} else {
temp.add(Calendar.MONTH, -6);
}
} else if (quarter==3) {
if (month==1) {
temp.add(Calendar.MONTH, -5);
} else if (month==2) {
temp.add(Calendar.MONTH, -4);
} else {
temp.add(Calendar.MONTH, -3);
}
} else {
if (month==1) {
temp.add(Calendar.MONTH, -2);
} else if (month==2) {
temp.add(Calendar.MONTH, -1);
} else {
}
}
Date tempDate = temp.getTime();
return isoSDF.format(tempDate);
}
public Date getQuarterStartDateAsDate(int quarter) throws Exception {
Calendar temp = (Calendar) fyCalStart.clone();
quarter = validateQuarter(quarter);
if (quarter==1) {
} else if (quarter==2) {
temp.add(Calendar.MONTH, 3);
} else if (quarter==3) {
temp.add(Calendar.MONTH, 6);
} else {
temp.add(Calendar.MONTH, 9);
}
Date tempDate = temp.getTime();
return tempDate;
}
public Date getQuarterEndDateAsDate(int quarter) throws Exception {
Calendar temp = (Calendar) fyCalEnd.clone();
quarter = validateQuarter(quarter);
if (quarter==1) {
temp.add(Calendar.MONTH, -9);
} else if (quarter==2) {
temp.add(Calendar.MONTH, -6);
} else if (quarter==3) {
temp.add(Calendar.MONTH, -3);
} else {
}
Date tempDate = temp.getTime();
return tempDate;
}
public Date getMonthStartDateAsDate(int quarter, int month) throws Exception {
Calendar temp = (Calendar) fyCalStart.clone();
quarter = validateQuarter(quarter);
month = validateMonth(month);
if (quarter==1) {
if (month==1) {
} else if (month==2) {
temp.add(Calendar.MONTH, 1);
} else {
temp.add(Calendar.MONTH, 2);
}
} else if (quarter==2) {
if (month==1) {
temp.add(Calendar.MONTH, 3);
} else if (month==2) {
temp.add(Calendar.MONTH, 4);
} else {
temp.add(Calendar.MONTH, 5);
}
} else if (quarter==3) {
if (month==1) {
temp.add(Calendar.MONTH, 6);
} else if (month==2) {
temp.add(Calendar.MONTH, 7);
} else {
temp.add(Calendar.MONTH, 8);
}
} else {
if (month==1) {
temp.add(Calendar.MONTH, 9);
} else if (month==2) {
temp.add(Calendar.MONTH, 10);
} else {
temp.add(Calendar.MONTH, 11);
}
}
Date tempDate = temp.getTime();
return tempDate;
}
public Date getMonthEndDateAsDate(int quarter, int month) throws Exception {
Calendar temp = (Calendar) fyCalEnd.clone();
quarter = validateQuarter(quarter);
month = validateMonth(month);
if (quarter==1) {
if (month==1) {
temp.add(Calendar.MONTH, -11);
} else if (month==2) {
temp.add(Calendar.MONTH, -10);
} else {
temp.add(Calendar.MONTH, -9);
}
} else if (quarter==2) {
if (month==1) {
temp.add(Calendar.MONTH, -8);
} else if (month==2) {
temp.add(Calendar.MONTH, -7);
} else {
temp.add(Calendar.MONTH, -6);
}
} else if (quarter==3) {
if (month==1) {
temp.add(Calendar.MONTH, -5);
} else if (month==2) {
temp.add(Calendar.MONTH, -4);
} else {
temp.add(Calendar.MONTH, -3);
}
} else {
if (month==1) {
temp.add(Calendar.MONTH, -2);
} else if (month==2) {
temp.add(Calendar.MONTH, -1);
} else {
}
}
Date tempDate = temp.getTime();
return tempDate;
}
/**
* Gets the current FY year based on start year
* @return
*/
public String getFYName() {
return fyName;
}
/**
* Gets a systematic name for the FY year plus quarter
* @param quarter
* @return
* @throws Exception
*/
public String getQuarterName(int quarter) throws Exception {
quarter=validateQuarter(quarter);
return fyName+".Q"+quarter;
}
/**
* Gets a systematic name for the FY year, quarter and month
* @param quarter
* @param month
* @return
* @throws Exception
*/
public String getMonthName(int quarter, int month) throws Exception {
quarter=validateQuarter(quarter);
month=validateMonth(month);
return fyName+".Q"+quarter+".M"+month;
}
public int validateQuarter(int quarter) throws Exception {
int [] test = {1,2,3,4};
for(int i:test) {
if (i==quarter) {
return i;
}
}
throw new Exception("Invalid Quarter");
}
public int validateMonth(int month) throws Exception {
int [] test = {1,2,3};
for(int i:test) {
if (i==month) {
return i;
}
}
throw new Exception("Invalid Month");
}
/**
* Method tests if given a string format does it belong to the current fiscalyear
* Does not say if its a future or past just returns true or false
* @param fy
* @return
* @throws Exception
*/
public static boolean inCurrentFyWindow(String fy) throws Exception {
Date yearTest = new Date();
FiscalYear test = new FiscalYear(yearTest);
if (fy.length()==8 ||fy.length()==6) {
//FY21Q1M1
String fyName = fy.split("Q")[0];
if (test.getFYName().toLowerCase().equals(fyName.toLowerCase())) {
return true;
} else {
return false;
}
} else {
//FY21
if (test.getFYName().toLowerCase().equals(fy.toLowerCase())) {
return true;
} else {
return false;
}
}
}
/**
* Method tests if the string for FiscalYear being passed has passed or not based on the current FiscalYear
*
* This can test FY year, FY year, quarter and finally FY year, quarter and month combinations
*
* @param fy
* @return
* @throws Exception
*/
public static boolean hasWindowHappenedFully(String fy) throws Exception {
Date yearTest = new Date();
if (fy.length()==8) {
//FY21Q1M1
String fyName = "20" + fy.substring(2, 4);
int quarter = Integer.parseInt(fy.substring(5, 6));
int month = Integer.parseInt(fy.substring(7, 8));
FiscalYear yQM = new FiscalYear(Integer.parseInt(fyName)-1);
if (yearTest.after(yQM.getMonthEndDateAsDate(quarter, month))) {
return true;
} else {
return false;
}
} else if (fy.length()==6) {
//FY21Q1
String fyName = "20" + fy.substring(2, 4);
int quarter = Integer.parseInt(fy.substring(5, 6));
FiscalYear yQM = new FiscalYear(Integer.parseInt(fyName)-1);
if (yearTest.after(yQM.getQuarterEndDateAsDate(quarter))) {
return true;
} else {
return false;
}
} else {
//FY21
String fyName = "20" + fy.substring(2, 4);
FiscalYear yQM = new FiscalYear(Integer.parseInt(fyName)-1);
if (yearTest.after(yQM.getEndDateAsDate())) {
return true;
} else {
return false;
}
}
}
public static String getCurrentFiscalYear() throws Exception {
Date yearTest = new Date();
FiscalYear test = new FiscalYear(yearTest);
return test.getFYName();
}
public static String getCurrentFiscalYearQuarter() throws Exception {
Date yearTest = new Date();
FiscalYear test = new FiscalYear(yearTest);
for(int q=1; q<=4; q++ ) {
for(int m=1; m<=3; m++ )
if (test.getMonthStartDateAsDate(q, m).before(yearTest) && test.getMonthEndDateAsDate(q, m).after(yearTest)) {
return test.getFYName()+".Q"+q;
}
}
return test.getFYName();
}
public static String getCurrentFiscalYearQuarterMonth() throws Exception {
Date yearTest = new Date();
FiscalYear test = new FiscalYear(yearTest);
for(int q=1; q<=4; q++ ) {
for(int m=1; m<=3; m++ )
if (test.getMonthStartDateAsDate(q, m).before(yearTest) && test.getMonthEndDateAsDate(q, m).after(yearTest)) {
return test.getFYName()+".Q"+q+".M"+m;
}
}
return test.getFYName();
}
public static String getFiscalYear(Date year2) throws Exception {
FiscalYear test = new FiscalYear(year2);
return test.getFYName();
}
public static String getFiscalYearQuarter(Date year2) throws Exception {
FiscalYear test = new FiscalYear(year2);
for(int q=1; q<=4; q++ ) {
for(int m=1; m<=3; m++ )
if (test.getMonthStartDateAsDate(q, m).before(year2) && test.getMonthEndDateAsDate(q, m).after(year2) || year2.equals(test.getMonthStartDateAsDate(q, m)) || year2.equals(test.getMonthEndDateAsDate(q, m))) {
return test.getFYName()+".Q"+q+".M"+m;
}
}
return test.getFYName();
}
public static String getFiscalYearQuarterMonth(Date year2) throws Exception {
FiscalYear test = new FiscalYear(year2);
for(int q=1; q<=4; q++ ) {
for(int m=1; m<=3; m++ )
if (test.getMonthStartDateAsDate(q, m).before(year2) && test.getMonthEndDateAsDate(q, m).after(year2) || year2.equals(test.getMonthStartDateAsDate(q, m)) || year2.equals(test.getMonthEndDateAsDate(q, m))) {
return test.getFYName()+".Q"+q+".M"+m;
}
}
return test.getFYName();
}
}