static Generator byWeekNoGenerator(int[] weekNumbers, final DayOfWeek weekStart, final DateValue dtStart) {
		final int[] uWeekNumbers = Util.uniquify(weekNumbers);

		return new Generator() {
			int year = dtStart.year();
			int month = dtStart.month();
			/** number of weeks in the last year seen */
			int weeksInYear;
			/** dates generated anew for each month seen */
			int[] dates;
			/** index into dates */
			int i = 0;

			/**
			 * day of the year of the start of week 1 of the current year. Since
			 * week 1 may start on the previous year, this may be negative.
			 */
			int doyOfStartOfWeek1;

			{
				checkYear();
				checkMonth();
			}

			void checkYear() {
				//if the first day of January is weekStart, then there are 7
				//if the first day of January is weekStart + 1, then there are 6
				//if the first day of January is weekStart + 6, then there is 1
				DayOfWeek dowJan1 = TimeUtils.firstDayOfWeekInMonth(year, 1);
				int nDaysInFirstWeek = 7 - ((7 + dowJan1.getCalendarConstant() - weekStart.getCalendarConstant()) % 7);

				//number of days not in any week
				int nOrphanedDays = 0;

				/*
				 * According to RFC 2445:
				 * 
				 * Week number one of the calendar year is the first week which
				 * contains at least four (4) days in that calendar year.
				 */
				if (nDaysInFirstWeek < 4) {
					nOrphanedDays = nDaysInFirstWeek;
					nDaysInFirstWeek = 7;
				}

				/*
				 * Calculate the day of year (possibly negative) of the start of
				 * the first week in the year. This day must be of weekStart.
				 */
				doyOfStartOfWeek1 = nDaysInFirstWeek - 7 + nOrphanedDays;

				weeksInYear = (TimeUtils.yearLength(year) - nOrphanedDays + 6) / 7;
			}

			void checkMonth() {
				//the day of the year of the 1st day in the month
				int doyOfMonth1 = TimeUtils.dayOfYear(year, month, 1);

				//the week of the year of the 1st day of the month.  approximate.
				int weekOfMonth = ((doyOfMonth1 - doyOfStartOfWeek1) / 7) + 1;

				//the number of days in the month
				int nDays = TimeUtils.monthLength(year, month);

				//generate the dates in the month
				IntSet udates = new IntSet();
				for (int weekNo : uWeekNumbers) {
					if (weekNo < 0) {
						weekNo += weeksInYear + 1;
					}
					if (weekNo >= weekOfMonth - 1 && weekNo <= weekOfMonth + 6) {
						for (int d = 0; d < 7; ++d) {
							int date = ((weekNo - 1) * 7 + d + doyOfStartOfWeek1 - doyOfMonth1) + 1;
							if (date >= 1 && date <= nDays) {
								udates.add(date);
							}
						}
					}
				}
				dates = udates.toIntArray();
			}

			@Override
			boolean generate(DTBuilder builder) {
				/*
				 * This is a bit odd, since we're generating days within the
				 * given weeks of the year within the month/year from builder.
				 */
				if (year != builder.year || month != builder.month) {
					if (year != builder.year) {
						year = builder.year;
						checkYear();
					}
					month = builder.month;
					checkMonth();

					i = 0;
				}

				if (i >= dates.length) {
					return false;
				}
				builder.day = dates[i++];
				return true;
			}

			@Override
			public String toString() {
				return "byWeekNoGenerator";
			}
		};
	}
static Generator byWeekNoGenerator ( int [ ] weekNumbers , final DayOfWeek weekStart , final DateValue dtStart ) { final int [ ] uWeekNumbers = Util . uniquify ( weekNumbers ) ; return new Generator ( ) { int year = dtStart . year ( ) ; int month = dtStart . month ( ) ; int weeksInYear ; int [ ] dates ; int i = 0 ; int doyOfStartOfWeek1 ; { checkYear ( ) ; checkMonth ( ) ; } void checkYear ( ) { DayOfWeek dowJan1 = TimeUtils . firstDayOfWeekInMonth ( year , 1 ) ; int nDaysInFirstWeek = 7 - ( ( 7 + dowJan1 . getCalendarConstant ( ) - weekStart . getCalendarConstant ( ) ) % 7 ) ; int nOrphanedDays = 0 ; if ( nDaysInFirstWeek < 4 ) { nOrphanedDays = nDaysInFirstWeek ; nDaysInFirstWeek = 7 ; } doyOfStartOfWeek1 = nDaysInFirstWeek - 7 + nOrphanedDays ; weeksInYear = ( TimeUtils . yearLength ( year ) - nOrphanedDays + 6 ) / 7 ; } void checkMonth ( ) { int doyOfMonth1 = TimeUtils . dayOfYear ( year , month , 1 ) ; int weekOfMonth = ( ( doyOfMonth1 - doyOfStartOfWeek1 ) / 7 ) + 1 ; int nDays = TimeUtils . monthLength ( year , month ) ; IntSet udates = new IntSet ( ) ; for ( int weekNo : uWeekNumbers ) { if ( weekNo < 0 ) { weekNo += weeksInYear + 1 ; } if ( weekNo >= weekOfMonth - 1 && weekNo <= weekOfMonth + 6 ) { for ( int d = 0 ; d < 7 ; ++ d ) { int date = ( ( weekNo - 1 ) * 7 + d + doyOfStartOfWeek1 - doyOfMonth1 ) + 1 ; if ( date >= 1 && date <= nDays ) { udates . add ( date ) ; } } } } dates = udates . toIntArray ( ) ; } @ Override boolean generate ( DTBuilder builder ) { if ( year != builder . year || month != builder . month ) { if ( year != builder . year ) { year = builder . year ; checkYear ( ) ; } month = builder . month ; checkMonth ( ) ; i = 0 ; } if ( i >= dates . length ) { return false ; } builder . day = dates [ i ++ ] ; return true ; } @ Override public String toString ( ) { return "byWeekNoGenerator" ; } } ; }
Constructs a generator that yields each day in the current month that
falls in one of the given weeks of the year.
@param weekNumbers the week numbers (each value must be in range
[-53,53])
@param weekStart the day of the week that the week starts on
@param dtStart the start date
@return the generator