function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
karthick Jkarthick J 

Get Month from Week Number ?

I have value like '199924' where 1999 => Year and 24 is week.

I want to get the month of the week (24) and date of the weeks first day. How can i do this in Apex ?
Best Answer chosen by karthick J
pigginsbpigginsb
This will depend on what you know about the String values you will be working with. For example...
// if the year is always represented by the left-most four characters, then you can use the left() method...
String rawValue = '199924';
String yearValue = rawValue.left(4);
system.assertEquals('1999', yearValue);

// week number can be obtained by using the removeStart() string method after obtaining the year... String String weekValue = rawValue.removeStart(yearValue);
system.assertEquals('24', weekValue);

// the left method will work in this instance, even if the week number is sometimes represented by a single digit...
rawValue = '20014';
yearValue = rawValue.left(4);
weekValue = rawValue.removeStart(yearValue); 
system.assertEquals('2001', yearValue);
system.assertEquals('4', weekValue);

// my approach from here would be to find the start of the year, and perhaps that start of first week of the year, assuming the week that contains January 1st is week #1
Integer yearInt = Integer.valueOf(yearValue);
Integer weekNumber = Integer.valueOf(weekValue);

// build a Date instance of January 1 of that year, then of the start of that week...
Date startOfYear = Date.newInstance(yearInt, 1, 1);
Date startOfWeekOne = startOfYear.toStartOfWeek();
Date startOfWeekFour = startOfWeekOne.addDays(weekNumber * 7);

system.assertEquals(Date.newInstance(2001, 1, 28), startOfWeekFour);
If the year is ever represented as two characters, then you would need some way of determining how many of the starting characters represent the year value.

Please double-check my assuption regarding whether the start of week one is the start of the week that contains January 1st.

My recommendation would be to create a utility method that can return the date for you, when passing in the raw string value, then fully test this in a test method, passing in valid and invalid values. You'll need to determine what the response should be whenever the inputs are invalid.
 

All Answers

pigginsbpigginsb
This will depend on what you know about the String values you will be working with. For example...
// if the year is always represented by the left-most four characters, then you can use the left() method...
String rawValue = '199924';
String yearValue = rawValue.left(4);
system.assertEquals('1999', yearValue);

// week number can be obtained by using the removeStart() string method after obtaining the year... String String weekValue = rawValue.removeStart(yearValue);
system.assertEquals('24', weekValue);

// the left method will work in this instance, even if the week number is sometimes represented by a single digit...
rawValue = '20014';
yearValue = rawValue.left(4);
weekValue = rawValue.removeStart(yearValue); 
system.assertEquals('2001', yearValue);
system.assertEquals('4', weekValue);

// my approach from here would be to find the start of the year, and perhaps that start of first week of the year, assuming the week that contains January 1st is week #1
Integer yearInt = Integer.valueOf(yearValue);
Integer weekNumber = Integer.valueOf(weekValue);

// build a Date instance of January 1 of that year, then of the start of that week...
Date startOfYear = Date.newInstance(yearInt, 1, 1);
Date startOfWeekOne = startOfYear.toStartOfWeek();
Date startOfWeekFour = startOfWeekOne.addDays(weekNumber * 7);

system.assertEquals(Date.newInstance(2001, 1, 28), startOfWeekFour);
If the year is ever represented as two characters, then you would need some way of determining how many of the starting characters represent the year value.

Please double-check my assuption regarding whether the start of week one is the start of the week that contains January 1st.

My recommendation would be to create a utility method that can return the date for you, when passing in the raw string value, then fully test this in a test method, passing in valid and invalid values. You'll need to determine what the response should be whenever the inputs are invalid.
 
This was selected as the best answer