I need to get a list of dates whose days are "Monday" in a range of dates supplied, both inclusive.
For example:
If we pass the interval 01/05/2017 until 15/05/2017 the function should return:
01/05/2017
05/08/2017
05/15/2017
My code is as follows:
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
List<String> dates = new ArrayList<String>();
Calendar cini = Calendar.getInstance();
cini.setTime(formatter.parse(peticion.getDiaInicio()));
Calendar cfin = Calendar.getInstance();
cfin.setTime(formatter.parse(peticion.getDiaFin()));
while (cfin.after(cini)) {
if (cini.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
logger.info("---------->" + cini.getTime().toString() + " es Lunes ");
dates.add(formatter.format(cini.getTime()));
}
cini.add(Calendar.DATE, 1);
}
for(String c : dates){
System.out.println("Fecha: " + c);
}
With this code you are only returning the first two values:
01/05/2017
05/08/2017
Any suggestions?