class EntityUtil {

	public static int getInstanceSequence(Date startTime, Frequency frequency, TimeZone tz, Date instanceTime) {
		if (startTime.after(instanceTime))
			return -1;

		Calendar startCal = Calendar.getInstance(tz);
		startCal.setTime(startTime);

		int count = 0;
		switch (frequency.getTimeUnit()) {
		case months:
			count = (int) ((instanceTime.getTime() - startTime.getTime()) / MONTH_IN_MS);
			break;
		case days:
			count = (int) ((instanceTime.getTime() - startTime.getTime()) / DAY_IN_MS);
			break;
		case hours:
			count = (int) ((instanceTime.getTime() - startTime.getTime()) / HOUR_IN_MS);
			break;
		case minutes:
			count = (int) ((instanceTime.getTime() - startTime.getTime()) / MINUTE_IN_MS);
			break;
		default:
		}

		if (count > 2) {
			startCal.add(frequency.getTimeUnit().getCalendarUnit(), (count / frequency.getFrequency()) * frequency.getFrequency());
			count = (count / frequency.getFrequency());
		} else {
            count = 0;
        }
		while (startCal.getTime().before(instanceTime)) {
			startCal.add(frequency.getTimeUnit().getCalendarUnit(), frequency.getFrequency());
			count++;
		}
		return count + 1;
	}

}