An interesting problem, although I have little experience with Joda. I tried to solve it using jata.util Classes with simple calculations, I think it solves your problem - only without Joda.
The method of accumulating all weekly numbers for a month in a year:
public static Integer[] getWeeksInMonth(int month, int year) { List<Integer> list = new ArrayList<Integer>(); SimpleDateFormat format = new SimpleDateFormat("w"); Calendar startDate = Calendar.getInstance(); Calendar endDate = Calendar.getInstance(); startDate.set(year, month - 1, 1); startDate.setMinimalDaysInFirstWeek(4); endDate.set(year, month, 0); // Iterate between the start and stop days while (startDate.getTimeInMillis() <= endDate.getTimeInMillis()) { Calendar cal = Calendar.getInstance(); cal.setTime(startDate.getTime()); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // System.out.println("Here here " + // cal.get(Calendar.DAY_OF_MONTH)); // Start of the week is Monday - check if dates 1, 2, 3 fall after // Wednesday (specified 4 day rule) if ((startDate.get(Calendar.DAY_OF_WEEK) > Calendar.THURSDAY) && (startDate.get(Calendar.DAY_OF_MONTH) < 4)) { // If they do, move the dates to next immediate start of the // week - making it to 4th startDate.add(Calendar.DAY_OF_MONTH, (4 - startDate.get(Calendar.DAY_OF_MONTH))); // Similarly - check if last dates of the month make less than 4 // for that month } else if ((startDate.get(Calendar.DAY_OF_MONTH) > 25) && ((startDate.getActualMaximum(Calendar.DAY_OF_MONTH) - (cal .get(Calendar.DAY_OF_MONTH))) + 1) < 4) { // If they do, move the dates to next immediate start of the // week - making it to next month startDate .add(Calendar.DAY_OF_MONTH, (startDate .getActualMaximum(Calendar.DAY_OF_MONTH) - startDate .get(Calendar.DAY_OF_MONTH)) + 1); } else { // Get the number of the week // System.out.println(startDate.getTime()); int week = Integer.parseInt(format.format(startDate.getTime())); // If December, check if the next year 1st week falls under // the 4 day rule, if it does then ignore if (!(startDate.get(Calendar.MONTH) == Calendar.DECEMBER && week == 1)) list.add(week); startDate.add(Calendar.WEEK_OF_MONTH, 1); } } return list.toArray(new Integer[list.size()]); }
The main method call:
public static void main(String... args) { int year = 2014; System.out.println(":::: YEAR " + year + " ::::"); for (int j = 1; j <= 12; j++) { Integer[] array = getWeeksInMonth(j, year); System.out.print("For month : " + j); System.out.print(" [ "); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.print("]"); System.out.println(); } }
Output (possible test cases):
:::: YEAR 2014 :::: For month : 1 [ 1 2 3 4 5 ] For month : 2 [ 6 7 8 9 ] For month : 3 [ 10 11 12 13 ] For month : 4 [ 14 15 16 17 ] For month : 5 [ 18 19 20 21 22 ] For month : 6 [ 23 24 25 26 ] For month : 7 [ 27 28 29 30 31 ] For month : 8 [ 32 33 34 35 ] For month : 9 [ 36 37 38 39 ] For month : 10 [ 40 41 42 43 44 ] For month : 11 [ 45 46 47 48 ] For month : 12 [ 49 50 51 52 ]
You can check other test cases and let me know if something is wrong. I know a lot of calculations, but it is flexible with requirements.
source share