Find Quarter to which a particular date belong for a Fiscal year

前端 未结 3 1481
傲寒
傲寒 2021-01-29 12:44

I am using java 1.7, How to find the quarter to which a particular date belong for a Fiscal year, which can start from 1st of any month (JAN-DEC) and also need the start date

3条回答
  •  孤独总比滥情好
    2021-01-29 13:14

    Use a calendar:

    Calendar cal = Calendar.getInstance();
    cal.setTime(myDate);
    

    Quarter:

    int q = cal.get(Calendar.MONTH)%3;
    System.out.println("Quarter: Q" + (q + 1));
    

    Begining of quarter:

    cal.set(Calendar.MONTH, 3*q);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    Date start = cal.getTime();
    System.out.println("Start: " + start);
    

    End of quarter:

    cal.add(Calendar.MONTH, 3);
    cal.add(Calendar.DATE, -1);
    Date end = cal.getTime();
    System.out.println("End: " + end);
    

提交回复
热议问题