Java 从当前日期获取当前财政年度 (YYYY-YYYY)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18616646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:33:42  来源:igfitidea点击:

Get Current Financial Year ( YYYY-YYYY) from Current Date

java

提问by Namita

I am trying to find out current financial year (FY - March to April) on the basis of current date in more efficient way. Here's what I have written so far

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

So if the current month is less than or equal to 3 (March) and year is 2013, FY should be = 2012-2013, if the month is 6(June) and year is 2013, FY should be = 2013-2014.

所以如果当月小于等于3(March),年份是2013,FY应该是=2012-2013,如果月份是6(June),年份是2013,FY应该是=2013-2014。

How do I achieve it?

我如何实现它?

回答by Adrian Shum

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

回答by pamphlet

Something like:

就像是:

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

回答by Umang Mehta

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

Just remove the extra functions.

只需删除额外的功能。

回答by Kevin Bowersox

It could be beneficial to make an object for the FiscalDateso you can reuse it throughout an application. I would avoid deprecated methods such as getMonth()and getYear()as others have suggested.

为 制作一个对象可能是有益的,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());
    }
}

回答by Gilbert Le Blanc

I suspect that one of the values needed is the fiscal month. The fiscal month is the month within the fiscal year. For instance, if the fiscal year starts in March, then March is the 0 month of the fiscal year. February is the 11 month of the fiscal year.

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

Here are some test results:

下面是一些测试结果:

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

Borrowing from Kevin Bowersox's answer, here's a FiscalDate class that gives the fiscal year and fiscal month, as well as the calendar year and calendar month. Both month values are zero based.

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

}

回答by APOORVA DOSHI

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

回答by nivas

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

        }

    }

}

回答by Dipin Krishnan

For those who are looking for a solution in JavaScript, here it is using MomentJSlibrary.

对于那些正在寻找 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

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