我应该使用哪个运算符比较两个LocalDate值?

时间:2019-05-07 12:54:55

标签: java localdate

我要提供LocalDate值,然后我要提供另一个值,但是我想确保新的值与以前的值不相同。我写了一些代码,但似乎没有用。

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...
}

我也尝试过关键字等于。但是同样,for循环会忽略比较。

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

在您回答之后,我尝试了您提出的许多解决方案。为了检查编译器是否进入for循环,我在控制台中放置了一条从未显示过的变量的打印消息。

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

4 个答案:

答案 0 :(得分:4)

您可以根据需要使用以下方法

    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));

这将返回

false
true
false
-2

答案 1 :(得分:2)

要比较两个本地日期,可以使用“ compareTo()”方法。遵循示例代码

   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));
   }
}

您的输出是

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

答案 2 :(得分:1)

您可以使用LocalDate方法或使用其他方法比较compareTo。这样做:

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"));
}

答案 3 :(得分:0)

我认为这是一个重复的问题,请参阅:How to compare two dates along with time in java。在您的示例中,您将希望进行比较

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