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


i trying find out current financial year (fy - march april) on basis of current date in more efficient way. here's have written far

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 current month less or equal 3 (march) , year 2013, fy should = 2012-2013, if month 6(june) , year 2013, fy should = 2013-2014.

how achieve it?

i suspect 1 of values needed fiscal month. fiscal month month within fiscal year. instance, if fiscal year starts in march, march 0 month of fiscal year. february 11 month of fiscal year.

here 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 kevin bowersox's answer, here's fiscaldate class gives fiscal year , fiscal month, calendar year , calendar month. both month values 0 based.

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

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -