我正在尝试以更有效的方式根据当前日期找出当前财政年度(FY - 3 月至 4 月)。这是我到目前为止所写的
public static void main(String[] args) {
int year = getYearFromDate(new Date());
System.out.println("Financial Year : " + year + "-" + (year+1));
System.out.println("Financial month : " + getMonthFromDate(new Date()));
}
private static int getMonthFromDate(Date date) {
int result = -1;
if (date != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
result = cal.get(Calendar.MONTH)+1;
}
return result;
}
public static int getYearFromDate(Date date) {
int result = -1;
if (date != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
result = cal.get(Calendar.YEAR);
}
return result;
}
因此,如果当前月份小于或等于 3(三月)且年份为 2013,则 FY 应为 = 2012-2013,如果月份为 6(六月)且年份为 2013,则 FY 应为 = 2013-2014。
我如何实现它?
原文由 Namita 发布,翻译遵循 CC BY-SA 4.0 许可协议
我怀疑所需的值之一是财政月份。会计月是会计年度中的月份。例如,如果会计年度从 3 月开始,则 3 月是会计年度的第 0 个月。 2 月是财政年度的第 11 个月。
下面是一些测试结果:
借用 Kevin Bowersox 的回答,这里有一个 FiscalDate 类,它给出了财政年度和财政月份,以及日历年和日历月。两个月份的值都是从零开始的。