我试图以更有效的方式根据当前日期找出当前财政年度(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(3月)且年份为2013,则FY应为2012-2013,如果月份为6(6月)且年份为2013,则FY应为= 2013-2014。

我该如何实现?

最佳答案

我怀疑所需的值之一是财政月。会计月份是会计年度内的月份。例如,如果会计年度从3月开始,则3月是会计年度的0个月。 2月是会计年度的11个月。

以下是一些测试结果:

Current Date : Wed Sep 04 14:23:17 EDT 2013
Fiscal Years : 2013-2014
Fiscal Month : 6

Current Date : Fri Feb 01 00:00:00 EST 2013
Fiscal Years : 2012-2013
Fiscal Month : 11

Current Date : Wed Jul 25 00:00:00 EDT 2012
Fiscal Years : 2012-2013
Fiscal Month : 4


Kevin Bowersox's answer借用,这是一个FiscalDate类,它给出了会计年度和会计月份,以及日历年度和日历月份。两个月的值都是从零开始的。

import java.util.Calendar;
import java.util.Date;

public class FiscalDate {

    private static final int    FIRST_FISCAL_MONTH  = Calendar.MARCH;

    private Calendar            calendarDate;

    public FiscalDate(Calendar calendarDate) {
        this.calendarDate = calendarDate;
    }

    public FiscalDate(Date date) {
        this.calendarDate = Calendar.getInstance();
        this.calendarDate.setTime(date);
    }

    public int getFiscalMonth() {
        int month = calendarDate.get(Calendar.MONTH);
        int result = ((month - FIRST_FISCAL_MONTH - 1) % 12) + 1;
        if (result < 0) {
            result += 12;
        }
        return result;
    }

    public int getFiscalYear() {
        int month = calendarDate.get(Calendar.MONTH);
        int year = calendarDate.get(Calendar.YEAR);
        return (month >= FIRST_FISCAL_MONTH) ? year : year - 1;
    }

    public int getCalendarMonth() {
        return calendarDate.get(Calendar.MONTH);
    }

    public int getCalendarYear() {
        return calendarDate.get(Calendar.YEAR);
    }

    public static void main(String[] args) {
        displayFinancialDate(Calendar.getInstance());
        displayFinancialDate(setDate(2013, 1, 1));
        displayFinancialDate(setDate(2012, 6, 25));
    }

    private static Calendar setDate(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        return calendar;
    }

    private static void displayFinancialDate(Calendar calendar) {
        FiscalDate fiscalDate = new FiscalDate(calendar);
        int year = fiscalDate.getFiscalYear();
        System.out.println("Current Date : " + calendar.getTime().toString());
        System.out.println("Fiscal Years : " + year + "-" + (year + 1));
        System.out.println("Fiscal Month : " + fiscalDate.getFiscalMonth());
        System.out.println(" ");
    }

}

10-06 12:40