Rotate Monthly

How to Pick an item from a collection based on a Rotation Strategy Weekly or Monthly

Lets say you have a requirement to display a unique url link every month or week from a collection of url links. For eg: If you have a collection of 4 url links and you want to pick 1 every month.

January ->
February ->
March ->
April ->
May ->
November ->
December ->


week1 of the year ->
week2 of the year ->
week5 of the year ->

Following is one better way in java that you can implement using enums. Create an enum class as below

package com.concepts.techgry;
import java.util.Calendar;
import java.util.List;
import static org.apache.commons.collections.CollectionUtils.isEmpty;
* @author
* @created 02/02/2021
* Purpose : To pick a unique item from a given list per month
* or per week based on rotation strategy.
* NOTE: Calendar.MONTH is zero indexed
* Calendar.WEEK_OF_YEAR is 1 indexed
public enum RotationStrategyEnum {
WEEKLY(Calendar.getInstance().get((Calendar.WEEK_OF_YEAR) - 1)),
int strategyIdx;
RotationStrategyEnum(int strategyIdx) {
this.strategyIdx = strategyIdx;
* Based on the int value of month/week, month % listSize
* will always give you an index within the list which is unique per month/week
* eg : listSize=4, January = 0, 0%4 =0, idx=0;
* listSize=4, February = 1, 1%4 =1, idx=1;
* Same logic applies to int value of week in an year when strategy is WEEKLY
public <T> T getItem(List<T> list) {
if(isEmpty(list)) return null;
int idx = this.strategyIdx % list.size();
return list.get(idx);

Now from client code you can pass the collection to the enum as follows and get a unique item from an collection of objects. Since the RotationStrategyEnum above is using generics, you are not restricted for only strings but you can pretty much
pass any type of collection to the enum to get a unique item from that collection based on RotationStrategy Weekly or Monthly.

List<String> collection = new ArrayList(Arrays.asList( "" , "" , "" )
String monthlyUniqueUrlLink = RotationStrategyEnum.MONTHLY.getItem(collection);
String weeklyUniqueUrlLink = RotationStrategyEnum.WEEKLY.getItem(collection);

Now you can pass the above links monthlyUniqueUrlLink or weeklyUniqueUrlLink to your front-end so you can display a unique rotated link based on the Rotation Strategy.