Test date at least one day earlier

System that controls work orders, the moment I open a new work order call I close the previous order. If the previous work order was opened on an earlier date(at least one day later) I close it at the end of the previous day's work order and open the new one at the beginning of the current day's work order.

What I'm wanting with this, a way to know if the date in question is at least one day earlier. I thought I'd test the time difference between the end of the previous day's work with the beginning of the current day's work, but it does not seem to me a good solution.

I made a test with calendar as follows:

Calendar diaAnterior = DATA_HORA_INICIAL_OS;
diaAnterior.add(Calendar.DATE, 1);
  if (DATA_HORA_ATUAL.get(Calendar.DATE) >= diaAnterior.get(Calendar.DATE)) {
    return true;
  }
return false;

In this way it only tests the day in question and if it was opened on the 30th of the month and today is Day 1, the method would return me an invalid information, or if I went on vacation and returned at the beginning of the month, the same error would happen.

So how can I test if the date from the opening of the work order is at the minimum of the previous day?

Author: Paulo H. Hartmann, 2017-04-12

1 answers

By zeroing the hour attributes, you will have exactly midnight today. With this, you can check if the date of the parameter is earlier than today.

public static void main(String[] args) {
    Calendar dataOrdem = Calendar.getInstance();
    // coloca a data em 31/03
    dataOrdem.set(Calendar.DAY_OF_MONTH, 31);
    dataOrdem.set(Calendar.MONTH, 2); // 2 = março

    System.out.println(testarDiaAnterior(dataOrdem));

  }

  private static boolean testarDiaAnterior(Calendar dataOrdem){
    Calendar hoje = Calendar.getInstance();
    hoje.set(Calendar.HOUR, 0);
    hoje.set(Calendar.MINUTE, 0);
    hoje.set(Calendar.SECOND, 0);
    hoje.set(Calendar.MILLISECOND, 0);
    return hoje.after(dataOrdem);
  }

Obs.: You can also use the before:

return dataOrdem.before(hoje);

But because it is a parameter, at some point the method may receive null.

 3
Author: Jefferson Borges, 2017-04-12 23:29:32