Problems comparing dates with Calendar

Asked

Viewed 989 times

5

I am developing a system for hotel and reservation I need the date of entry is greater than or equal to that of the reservation, and the exit is greater than that of the entrance.
I made these conditions, but when I test the reservation date and entry date equal I can not make the registration.
I don’t know what could be wrong with my method code. 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;    
    }

2 answers

5

I think this is what you’re looking for:

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

If the date of entry is equal to or after the date of reservation, it is checks whether this is prior to the date of departure. 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 are really using version 8 of Java, you can replace Calendar for LocalDateTime, similar to Joda Time.

3

One of the ways to make this comparison is by using the method compareTo, evaluating their 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 argument;
  • returns -1 if the date on the left is earlier than the date passed as 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


}

Returning:

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

Functioning in the IDEONE.

The first comparison, the three dates perfectly meet your question condition, in the second comparison, the date of entry is after the reservation date and in the third comparison, the date of departure is before the date of entry.

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

  • in the third comparison, the date of departure is earlier than the date of entry. Obg Diego F, but you said that in the third comparison the departure date is earlier... but I put later... I tested with IDEONE’s if, and I still can’t register with the same reservation and entry dates.

  • @Lizy yes, as I said in the answer, there I did 3 demonstrations so that you could observe that it is validating correctly, according to the conditions you mentioned in the question. Note that you only enter if in the first condition, which is where the check-in date is later than the booking date and earlier than the check-out date;

  • @Lizy this solution did not serve you? Let me know if you are having problem for me to review.

  • I tested the suggestion that you gave me, but I still can not register when the date of reservation and entry date are equal, ie, Reserve date = 23/04/2016, Date Entry 23/04/2016 and exit date = 24/04/2016 as example.

  • 1

    @Lizy fix it in ideone, see that it works perfectly.

  • @Lizy still not working?

  • Hello @Diego F, not yet! The other functions do work but the option to put the same date for a reservation and input date does not work, displays error message! I need to exist this option of the booking date and entry be on the same day, month, year! Obg!

  • @Is Lizy an application made in swing? Could you provide the code of this screen? Maybe the problem is elsewhere in the code.

  • Yes it is an application made in java swing!!! I will send you the code!!!

  • @Lizy seems that you had added in my reply and the staff rejected it. Paste the code here Pastebin.com and return me with the link.

  • Hello @Diego F I appreciate the collaboration but I already solved the problem, and I’m sorry for the mistake, I’m new here... I found a pc complicated edit to paste, but now td well! Obg!

Show 6 more comments

Browser other questions tagged

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