我应该使用哪个运算符来比较两个 LocalDate 值?
Which operator should I use to compare two LocalDate values?
我正在提供一个 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...
}
我也试过关键字equals。但是 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;
}
}
您可以根据需要使用以下方法
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));
这将 return
false
true
false
-2
您可以使用 compareTo
方法比较 LocalDate
或使用不同的方法进行比较。这样做:
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"));
}
我认为这是一个重复的问题,请参阅:How to compare two dates along with time in java。在您的示例中,您可能希望为 compare
执行此操作
for (Flight flight1 : flightList) {
if (flight1.getDateOfDeparture().compareto(flight.getDateOfDeparture()) != 0)) {
theDay = false;
}
}
要比较两个本地日期,您可以使用 "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
我正在提供一个 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...
}
我也试过关键字equals。但是 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;
}
}
您可以根据需要使用以下方法
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));
这将 return
false
true
false
-2
您可以使用 compareTo
方法比较 LocalDate
或使用不同的方法进行比较。这样做:
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"));
}
我认为这是一个重复的问题,请参阅:How to compare two dates along with time in java。在您的示例中,您可能希望为 compare
执行此操作for (Flight flight1 : flightList) {
if (flight1.getDateOfDeparture().compareto(flight.getDateOfDeparture()) != 0)) {
theDay = false;
}
}
要比较两个本地日期,您可以使用 "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