Test date of at least one previous day

2

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

What I'm wanting with this, a way of knowing if the date in question is at least a day earlier. I thought about testing the difference in hours between the end of the day's workday and the start of the current day's work, but that does not seem to be a good solution.

I ran 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;

But 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 will return an invalid information, or if you went on vacation and returned at the beginning of the month, the same error would occur.

So, how can I test if the opening date of the service order is at least the previous day?

    
asked by anonymous 12.04.2017 / 21:08

1 answer

3

Zeroing the attributes of hours, you will have exactly the midnight of 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);
  }

Note: You can also use the before method:

return dataOrdem.before(hoje);

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

    
12.04.2017 / 23:27