Get all "Monday" in a range of dates

6

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?

    
asked by Joacer 06.07.2017 в 14:45
source

3 answers

5

The problem is the condition of while . When it reaches the third iteration, cini=15/05/2017 , cini and cfin are equal, which is not met cfin.after(cini) .

A possible solution would be to change the condition to

while (!cfin.before(cini)) {
    
answered by 06.07.2017 / 14:50
source
5

In your condition while you have to add another question, if cfin equals cini:

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");    
        List<String> dates = new ArrayList<String>();
        Calendar cini = Calendar.getInstance();
        try {
            cini.setTime(formatter.parse("1/05/2017"));

            Calendar cfin = Calendar.getInstance();
            cfin.setTime(formatter.parse("15/05/2017"));
            while (cfin.after(cini) || cfin.equals(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);
            }
        } 
        catch (ParseException e) {
            e.printStackTrace();
        }

This way you make sure that you compare the last given date

    
answered by 06.07.2017 в 15:09
1

Seeing that nobody has picked up the glove ...

int diaInicial = cini.get(Calendar.DAY_OF_WEEK);
int diferenciaDias = Calendar.MONDAY - diaInicial; // dias que faltan para el primer lunes.
if (diferenciaDias < 0) {
   diferenciaDias += 6;
}
cini.add(Calendar.DATE, diferenciaDias); // cini está en el primer lunes
while (!cfin.before(cini)) {
   dates.add(formatter.format(cini.getTime());
   cini.add(Calendar.DATE, 7); // Siguiente lunes
}

A bit more generalized here: link

    
answered by 06.07.2017 в 19:04