Problems comparing dates with Calendar

4

I am developing a system for hotel and in Reservation precise that the date of entrance is greater or equal than the one of the reservation, and the one of the exit is greater than the one of the entrance. I made these conditions, but when I prove the date of reservation and the same date of entry I can not register.
I do not know what might be wrong with my code in my method. Could someone help me ??

private boolean verificaData(Calendar dcDataReserva, JDateChooser dcDataEntrada, JDateChooser dcDataSaida){

      Calendar Data_Reserva = Calendar.getInstance();
      Calendar Data_Entrada = dcDataEntrada.getCalendar();
      Calendar Data_Saida = dcDataSaida.getCalendar();
      boolean data;

      if(Data_Entrada.before(Data_Reserva) && (Data_Saida.before(Data_Entrada))){
          data= false;
      }else
          if((Data_Entrada.after(Data_Reserva)) && (Data_Saida.after(Data_Entrada))){
          data = true; 
          }else

              if(Data_Reserva.equals(Data_Entrada)){
                data = true;
              }else{
                data = false;
             }

              return data;    
    }
    
asked by anonymous 23.04.2016 / 23:45

2 answers

4

I think this is what you are looking for:

private boolean verificaData(Calendar dataReserva, Calendar dataEntrada, Calendar dataSaida){
    return (dataEntrada.equals(dataReserva) || dataEntrada.after(dataReserva)) && dataEntrada.before(dataSaida);
}

If the check-in date is the same or after the date of the reservation, it is checked if it is prior to the check-out date. If the first condition is false, there is no need to check if the exit date is correct.

I saw that the question was tagged with Java8. If you're even using Java version 8, you can replace Calendar with LocalDateTime , similar to Joda Time.

    
24.04.2016 / 01:36
3

One of the ways to do this comparison is to use the compareTo method, evaluating your return:

return (dataEntrada.compareTo(dataReserva) >= 0 && dataSaida.compareTo(dataEntrada) > 0)

The return of compareTo can be:

  • equal to 0 if both dates are equal;
  • returns 1 (greater than 0) if the leftmost date is later than the date passed as an argument;
  • returns -1 if the date on the left is earlier than the date passed as an argument.

Example:

public static String validarDataDate(Calendar dataReserva, Calendar dataEntrada, Calendar dataSaida) {

    if(dataEntrada.compareTo(dataReserva) >= 0 && dataSaida.compareTo(dataEntrada) > 0){
        return "tudo certo";
    }else{
        return "algumas datas sao invalidas";
    }
}

public static void main (String[] args) throws java.lang.Exception
{
    Date dataReserva = new SimpleDateFormat("dd/MM/yyyy").parse("20/04/2016");
    Date dataEntrada = new SimpleDateFormat("dd/MM/yyyy").parse("22/04/2016");
    Date dataSaida = new SimpleDateFormat("dd/MM/yyyy").parse("27/04/2016");;
    Calendar cal1 =  Calendar.getInstance();
    Calendar cal2 =  Calendar.getInstance();
    Calendar cal3 =  Calendar.getInstance();
    cal1.setTime(dataReserva);
    cal2.setTime(dataEntrada);
    cal3.setTime(dataSaida);
    System.out.println(validarDataDate(cal1, cal2, cal3));// reserva menor que entrada que é menor que saida
    System.out.println(validarDataDate(cal2, cal2, cal3));// reserva igual a entrada que é menor que saida
    System.out.println(validarDataDate(cal2, cal1, cal3));// reserva maior que entrada que é menor que saida
    System.out.println(validarDataDate(cal1, cal3, cal2));// reserva menor que entrada que é maior que saida


}

That returns:

  

all right
  all right   some dates are invalid
  some dates are invalid

Running on IDEONE .

The first comparison, the three dates perfectly match your question condition, in the second comparison, the date of entry is later than the date of reservation and in the third comparison, the date of exit is earlier than the date of entry.

In this answer there is also another way to compare dates using the new

24.04.2016 / 00:19