DESKTOP-ESBC3OR_20200903-蒋生桃
问题
Comparator<Customer> comparator = new Comparator<Customer>() {
@Override
public int compare(Customer o1, Customer o2) {
// The operator > is undefined for the argument type(s) java.time.LocalDate, java.time.LocalDate
if (o1.getBirthdate() > o2.getBirthdate() ) {
return -1;
} else if (o1.getBirthdate() == o2.getBirthdate()) {
return 0;
} else {
return 1;
}
}
};
为什么o1.getBirthdate() > o2.getBirthdate()
编译错误?
解决
因为getBirthdate()
获取的是年、月、日,不能直接用 > 比较
应该用以下比较方法
Comparator<Customer> comparator = new Comparator<Customer>() {
@Override
public int compare(Customer o1, Customer o2) {
if (o1.getBirthdate().isAfter(o2.getBirthdate())) {
return -1;
} else if (o1.getBirthdate() == o2.getBirthdate()) {
return 0;
} else {
return 1;
}
}
};
吐槽
爱护自己,不要感冒
近期评论