Return existing dates between two other dates - Android

Asked

Viewed 109 times

1

I would like to know if there is any class or library that returns the existing dates between two dates that I pass.

For example: would pass the dates 23/02/2016 and 25/02/2016 and return me the date 24/02/2016.

I saw many methods teaching to return the amount of dates existing between two dates, that would not be what I need, and I imagine creating a class to do what I’m asking would take a lot of work, so I decided to take a chance to see if anyone knows if there’s any way to do it.

  • Return randomly or always will be with this difference from the example?

  • These dates are in literal value? String even???

  • And if the entry is 20/02/2016 and 25/02/2016 ?

  • The dates can be in any value at the end can convert to what I will use, in the case being String, the return value would be in ascending order of the dates. In the case cited by Pablo he would return: 21/02/2016, 22/02/2016, 23/02/2016 and 24/02/2016. The return may also include the dates passed in the case then would be: 20/02/2016, 21/02/2016, 22/02/2016, 23/02/2016, 24/02/2016 and 25/02/2016.

1 answer

2


Try the following:

EDITED

As reported in the commentary, there is a need to decrease 1 in the month to set a Date (String) in a Calendar.

Example:

final String[] valI = inicio.split("/");
cINI.set(Calendar.DAY_OF_MONTH, Integer.valueOf(valI[0])-1);

Thinking about this and in order to simplify the code a little, follow the modified method:

public static List<String> diferencaDeDatas(String inicio, String fim) throws ParseException{
    // Tranforma Date em String e vice-versa
    final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    Calendar c = Calendar.getInstance(); 
    // Clonamos os Calendar para não ter a mesma referencia
    Calendar cINI = (Calendar) c.clone();
    Calendar cFIM = (Calendar) c.clone();

    //Transformamos a STring em java.util.Date
    Date dtIni = sdf.parse(inicio);
    Date dtFim = sdf.parse(fim);

    // Setamos dos java.util.Date nos Calendar's 
    cINI.setTimeInMillis(dtIni.getTime());
    cFIM.setTimeInMillis(dtFim.getTime());

    // Se a data final for menor que a maior ou igual  retorna uma lista vazia...
    if(cFIM.getTimeInMillis() <= cINI.getTimeInMillis()){
        return new ArrayList<>(0);
    }
    // Lista que vamos retonar com o valores
    List<String> itens = new ArrayList<>(0);

    // adicionamos +1 dia, pois não iremos contar o dia inicial
    cINI.set(Calendar.DAY_OF_MONTH, cINI.get(Calendar.DAY_OF_MONTH)+1);

    // vamos realizar a acão enquanto a data inicial for menor q a final
    while(cINI.getTimeInMillis() < cFIM.getTimeInMillis()){
        // adicionamos na lista...
        itens.add(sdf.format(cINI.getTime()));
        // adicionamos +1 dia....
        cINI.set(Calendar.DAY_OF_MONTH, cINI.get(Calendar.DAY_OF_MONTH)+1);
    }

    return itens;
}
  • It worked perfectly, thank you very much man

  • 1

    Oops, I didn’t notice a small problem with your code, when you set the month of the Calendar class you need to deduct 1 in the value of the String month that was passed, because for the Calendar the months start at 0 and go up to 11, then the way it is the code returns the dates with a month more than the dates you passed. Then the two lines of code would be: Cini.set(Calendar.MONTH, Integer.valueOf(Vali[1]) - 1); cFIM.set(Calendar.MONTH, Integer.valueOf(Vali[1])- 1);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.