0

I have the following problem: I need to get the week year of a fiscal year, but the thing is that I can have a dynamic fiscal year. For instance, instead of considering the end of my fiscal year December, 31, the end of my fiscal year will be August, 15.

So I should consider my year being from: August 16, (CurrentYear -1) until August 15, CurrentYear.

This way, I need to create a method passing some date, let say August 18, (CurrentYear-1) and this method should return the week of the year. In this case, it would be 1.

I know that with Java I can get the week year with something like:

Calendar cal = Calendar.getInstance();
cal.setTime(selectedDate.toDate());
int weekNumber = cal.get(Calendar.WEEK_OF_YEAR);

I'd like to know if there is some API that does this same math based in a dynamic "End of the year", or maybe some idea about how to do that.

Igor
  • 1,397
  • 3
  • 24
  • 56
  • 2
    Calculate the number of days from whatever your fiscal year day one is (in this case, August 16), then divide it by 7. Round up to the next whole number and that is your fical week. – Rabbit Guy Jan 18 '16 at 19:24

1 Answers1

0

Suggest:

Start off by defining the reference week for the first date of the fiscal year

Calendar cal = Calendar.getInstance();
cal.setTime(fiscalStartDate.toDate());
int referenceWeek = cal.get(Calendar.WEEK_OF_YEAR);

For any given date, the fiscal week will be:

cal.setTime(fiscalGivenDate.toDate());
int calendarWeek = cal.get(Calendar.WEEK_OF_YEAR)
int fiscalWeek = (calendarWeek > referenceWeek) ? calendarWeek- referenceWeek : (calendarWeek + <numberOfWeeks>) - referneceWeek;

I've used pseudocode here, to find "numberOfWeeks", check calculate number of weeks in a given year. The reason for this tweak is you have to correct once the date transitions the years (from e.g. 52 to 1), but you need to know if year has 52 or 53 weeks.

So, not elegant, but calendar stuff rarely is :-)

Community
  • 1
  • 1
Svea
  • 267
  • 1
  • 8