One Star

[resolved] How to display week number

Hi
I have a need to show CalendarYearweek and FiscalYearweek based on a configurable start date extending till 5000 days. Here Calendar Year means 1st Jan to 31st Dec and Fiscal Year means 1st July to 30th June.
For CalendarYearweek: I did it this way: Integer.parseInt(TalendDate.formatDate("w", row1.date)) when i do this, i get all week numbers accurately except for the last week of 2011. My custom start date is from 01st July 2010, Last week of 2010 i.e., Dates in YYYYMMDD - (20101226 to 20101231) is showing as 1 instead of 53.
For Fiscal Year: How do I calculate the Fiscal Year week assuming the financial year starts at July 1st of ever year?
Best Regards,
Kalyan

Accepted Solutions
Seven Stars

Re: [resolved] How to display week number

Hello,
You can do a little bit of math to calculate what you seem to want.  I've broken this up into multiple lines to make it more readable, but you could cram them into a single line:
`//get the date as a date.Date date = TalendDate.parseDate("yyyyMMdd","20101231");//get month as a number NOTE: this starts at 0 for January and goes to 11 for December.int month = TalendDate.getPartOfDate("MONTH",date);//get week of year*int weekOfYear = TalendDate.getPartOfDate("WEEK_OF_YEAR",date);//get our desired calendar year, 26 is chosen arbitrarily since it's simply the halfway point of a year.int calendarWeek = month==11 && weekOfYear < 26 ? weekOfYear+52 : weekOfYear;//get fiscal week.int fiscalWeek = calendarWeek > 26 && month>=6 ? calendarWeek-26 : calendarWeek+26;`

Will output the following for the dates included in the print statements:
` connecting to socket on port 4063 connectedmonth = 5various week stuff for date: 20100630Week of Year Calendar: 27 Fiscal: 53month = 6various week stuff for date: 20100701Week of Year Calendar: 27 Fiscal: 1month = 11various week stuff for date: 20101226Week of Year Calendar: 53 Fiscal: 27month = 11various week stuff for date: 20101231Week of Year Calendar: 53 Fiscal: 27month = 0various week stuff for date: 20100101Week of Year Calendar: 1 Fiscal: 27 disconnected`

Hope that helps!

All Replies
Seven Stars

Re: [resolved] How to display week number

Hello,
You can do a little bit of math to calculate what you seem to want.  I've broken this up into multiple lines to make it more readable, but you could cram them into a single line:
`//get the date as a date.Date date = TalendDate.parseDate("yyyyMMdd","20101231");//get month as a number NOTE: this starts at 0 for January and goes to 11 for December.int month = TalendDate.getPartOfDate("MONTH",date);//get week of year*int weekOfYear = TalendDate.getPartOfDate("WEEK_OF_YEAR",date);//get our desired calendar year, 26 is chosen arbitrarily since it's simply the halfway point of a year.int calendarWeek = month==11 && weekOfYear < 26 ? weekOfYear+52 : weekOfYear;//get fiscal week.int fiscalWeek = calendarWeek > 26 && month>=6 ? calendarWeek-26 : calendarWeek+26;`

Will output the following for the dates included in the print statements:
` connecting to socket on port 4063 connectedmonth = 5various week stuff for date: 20100630Week of Year Calendar: 27 Fiscal: 53month = 6various week stuff for date: 20100701Week of Year Calendar: 27 Fiscal: 1month = 11various week stuff for date: 20101226Week of Year Calendar: 53 Fiscal: 27month = 11various week stuff for date: 20101231Week of Year Calendar: 53 Fiscal: 27month = 0various week stuff for date: 20100101Week of Year Calendar: 1 Fiscal: 27 disconnected`

Hope that helps!