2

我正在尝试以更有效的方式根据当前日期找出当前财政年度(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。

我如何实现它?

4

11 回答 11

9

我怀疑需要的值之一是财政月份。会计月是会计年度内的月份。例如,如果财政年度从 3 月开始,则 3 月是财政年度的第 0 个月。二月是财政年度的第 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 的回答,这里有一个 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(" ");
    }

}
于 2013-09-04T18:30:19.273 回答
6

I wonder if you have really tried to solve it by yourself. It is so obvious and straight forward

psuedo code:

if ( monthOf(currentDate) >= MARCH) then
  FY =  yearOf(currentDate) + "-" + (yearOf(currentDate) +1);
else 
  FY = (yearOf(currentDate) - 1) + "-" + yearOf(currentDate);
于 2013-09-04T14:37:18.070 回答
5
int CurrentYear = Calendar.getInstance().get(Calendar.YEAR);
int CurrentMonth = (Calendar.getInstance().get(Calendar.MONTH)+1);
String financiyalYearFrom="";
String financiyalYearTo="";
if (CurrentMonth<4) {
    financiyalYearFrom="01-04-"+(CurrentYear-1);
    financiyalYearTo="31-03-"+(CurrentYear);
} else {
    financiyalYearFrom="01-04-"+(CurrentYear);
    financiyalYearTo="31-03-"+(CurrentYear+1);
}
于 2017-06-21T05:05:46.827 回答
4
public static void main(String[] args) {

    int year = Calendar.getInstance().get(Calendar.YEAR);

    int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
    System.out.println("Financial month : " + month);
    if (month < 3) {
        System.out.println("Financial Year : " + (year - 1) + "-" + year);
    } else {
        System.out.println("Financial Year : " + year + "-" + (year + 1));
    }
}

只需删除额外的功能。

于 2013-09-04T14:43:41.040 回答
3

为 制作一个对象可能是有益的,FiscalDate因此您可以在整个应用程序中重用它。我会避免不推荐使用的方法,例如getMonth()其他getYear()人建议的方法。

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


public class FiscalDate {

    private Date actual;
    private int month;
    private int year;

    public FiscalDate(Date date){
        this.actual = date;
        this.init();
    }

    private void init(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(this.actual);
        this.month = cal.get(Calendar.MONTH);
        int advance = (this.month <= 3) ? -1:0;
        this.year = cal.get(Calendar.YEAR) + advance;
    }

    public Date getActual() {
        return actual;
    }

    public void setActual(Date actual) {
        this.actual = actual;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public static void main(String[] args) {
        FiscalDate fDate = new FiscalDate(new Date());
        System.out.println(fDate.getYear());
    }
}
于 2013-09-04T14:50:20.073 回答
2

就像是:

Date d = new Date();
int y = d.getMonth() < 3 ? d.getYear() - 1 : d.getYear();

System.out.println("Financial Year : " + y + "-" + (y + 1));
System.out.println("Financial month : " + d.getMonth());
于 2013-09-04T14:37:40.613 回答
2

在 Java 中查找给定日期财政年度的简单解决方案

package testing;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

public class CurrentFinancialYear {

    public static void main(String[] args) throws ParseException {
        System.out.println(getActualFinancialYear("01-02-2022 23:10:10"));
    }       

    public static int getActualFinancialYear(String givendate) throws ParseException {
        
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        Date givenDate=formatter.parse(givendate);
        LocalDate localDate1 = givenDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        int year = localDate1.getYear();
        String fiscaldate = "31-03-" + year + " 23:59:59";
        Date fiscalDate = formatter.parse(fiscaldate);
        
        if (givenDate.before(fiscalDate)) {
            year = year - 1;
        }
        return year;
    }
}
于 2022-01-04T10:18:35.330 回答
1

Javascript

    let FY = document.getElementById("fYear");
    FY.innerHTML = new Date().getMonth() <= 9 ? "Fiscal Year "+new Date().getFullYear() : "Fiscal Year "+new Date().getFullYear() +1;
于 2021-02-11T19:43:35.653 回答
0

此代码允许您在给定的假设下设置周年日期并在不同的构造函数上设置会计年度。

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();
    }

}
于 2020-07-27T17:22:23.383 回答
0
public interface FinancialYearService {
     public int getFinancialYear();
     public int getFinancialMonth();
     public void generateFinancialYearMonth(int month,int year);
}

public class FinancialYearServiceImpl implements FinancialYearService {

    private int financialYear;
    private int financialMonth;

    private void setFinancialYear(int financialYear) {
        this.financialYear = financialYear;
    }

    private void setFinancialMonth(int financialMonth) {
        this.financialMonth = financialMonth;
    }

    @Override
    public int getFinancialYear() {

        return financialYear;
    }

    @Override
    public int getFinancialMonth() {

        return financialMonth;
    }

    /*
     * (non-Javadoc)
     * have to send current month and year to get financial month and year 
     */
    @Override
    public void generateFinancialYearMonth(int month,int year) {

        if(month <= 3 ){ 

          setFinancialMonth(month + 9);
          setFinancialYear(year - 1); 

        }
        else{

          setFinancialMonth(month - 3);
          setFinancialYear(year);

        }

    }

}
于 2018-04-04T08:00:54.263 回答
0

对于那些正在寻找 JavaScript 解决方案的人来说,这里使用的是MomentJS库。

let financialYear;
let today = moment();
if(today.month() >= 3){
    financialYear = today.format('YYYY') + '-' + today.add(1, 'years').format('YYYY')
}
else{
    financialYear = today.subtract(1, 'years').format('YYYY') + '-' + today.add(1, 'years').format('YYYY')
}
console.log(financialYear)

https://codepen.io/connect_dips/pen/QWWdwed

于 2019-10-21T13:40:58.197 回答