0

I am giving a LocalDate value and then I want to give another one but I want to make sure that the new one is not the same with the one before. I wrote some code but doesn't seem to be working.

Flight flight = new Flight();
System.out.println("Give the day of the departure.");
LocalDate day = LocalDate.of(2019, Month.MAY, scanner.nextInt());
flight.setDateOfDeparture(day);
boolean theDay = true; //Flag (reversed way in order to achieve the TRUE logic value).

for (Flight flight1 : flightList) {
    if (flight1.getDateOfDeparture() == (flight.getDateOfDeparture())) {
           theDay = false;
     }
}

if (theDay) {
    // Some logic...
}

I tried also the keyword equals. but again the for loop ignores the compare.

for (Flight flight1 : flightList) {
   if (flight1.getDateOfDeparture().equals(flight.getDateOfDeparture())) {
        theDay = false;
   }
}

After your answers I tried many of the solutions you proposed. To check if the compiler gets into for loop I put a print message of the variable which is never seen to my console.

for (Flight flight1 : flightList) {
   System.out.println(flight1.getDateOfDeparture());
   if(flight1.getDateOfDeparture().compareTo(flight.getDateOfDeparture())==0) {
      theDay = false;
    }
}
Michael Pnrs
  • 115
  • 11

4 Answers4

4

You can use below methods according to your needs

    LocalDate oldDate = LocalDate.of(2019,3,31);
    LocalDate newDate = LocalDate.of(2019,4, 1);
    System.out.println(oldDate.isAfter(newDate));
    System.out.println(oldDate.isBefore(newDate));
    System.out.println(oldDate.isEqual(newDate));
    System.out.println(oldDate.compareTo(newDate));

This will return

false
true
false
-2
Gagan Chouhan
  • 312
  • 1
  • 6
  • Tried the isEqual method but it keeps ignoring the compare. For example firstly I gave 15 as the day and then I gave again 15 but it continued into the if brackets afterwards. – Michael Pnrs May 07 '19 at 13:08
  • The problem is somewhere else..your answer might be correct..you can see the updated code. – Michael Pnrs May 07 '19 at 13:35
2

To compare two local date you can use "compareTo()" method. Follow the sample code

   import java.time.LocalDate;

public class Example {
   public static void main(String[] args) {
      /* Comparing the Dates given in String format
       * First the given strings are parsed in the LocalDate
       * and then compared against each other using compareTo()
       */
      LocalDate date1 = LocalDate.parse("2017-06-22");
      System.out.println(date1);  
      LocalDate date2 = LocalDate.parse("2017-06-22");
      System.out.println(date2);  
      System.out.println(date2.compareTo(date1)); 

      /* Comparing the Dates with the given year, month and
       * day information. The given data is passed in the of() method
       * of LocalDate to create instances of LocalDate and then compared
       * using the compareTo() method.
       * 
       */
      LocalDate date3 = LocalDate.of(2017, 06, 22);
      System.out.println(date3);  
      LocalDate date4 = LocalDate.of(2017, 10, 26);
      System.out.println(date4);  
      System.out.println(date4.compareTo(date3));

      /* Given Date is compared with the current Date
       */
      LocalDate date5 = LocalDate.now();
      System.out.println(date5);
      LocalDate date6 = LocalDate.of(2016, 02, 10);
      System.out.println(date6);
      System.out.println(date6.compareTo(date5));
   }
}

your output is

2017-06-22
2017-06-22
0
2017-06-22
2017-10-26
4
2017-10-28
2016-02-10
-1
1

You can compare LocalDates using the compareTo method or using different methods for comparison. Do it like this:

public static void main(String args[]) {
    LocalDate today = LocalDate.now();
    LocalDate yesterday = today.minusDays(1);
    LocalDate tomorrow = today.plusDays(1);

    int comparedToYesterday = today.compareTo(yesterday);
    int comparedToTomorrow = today.compareTo(tomorrow);
    int comparedToItself = today.compareTo(today);

    // using compareTo only
    System.out.println("Comparison to an earlier date results in: " + comparedToYesterday);
    System.out.println("Comparison to a later date results in: " + comparedToTomorrow);
    System.out.println("Comparison to the same date results in: " + comparedToItself);

    // using other comparison methods
    System.out.println("Is today after yesterday? Answer: " 
                        + (today.isAfter(yesterday) ? "yes" : "no"));
    System.out.println("Is today before tomorrow? Answer: "
                        + (today.isBefore(tomorrow) ? "yes" : "no"));
    System.out.println("Is today after today? Answer: "
                        + (today.isAfter(today) ? "yes" : "no"));
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
0

I believe this is a duplicate question, please see: How to compare two dates along with time in java. In your example you would want to do this for the compare

for (Flight flight1 : flightList) {
    if (flight1.getDateOfDeparture().compareto(flight.getDateOfDeparture()) != 0)) {
           theDay = false;
     }
}
B. Miller
  • 66
  • 3