This question already has answers here:
How to compare dates in Java? [duplicate]
                                
                                    (11个答案)
                                
                        
                                3年前关闭。
            
                    
我有两个约会

Date dateOfEnq = sdf.parse(ea.getDateOfEnquiry());
Date validDate = DateUtil.getAfterMonth(dateOfEnq, range);


现在我在做

if (dateOfEnq <= validDate) {
    ount++;
}


但这给了我错误

The operator <= is undefined for the argument type(s) java.util.Date, java.util.Date.


我如何比较这两个日期?

谢谢

最佳答案

java.util.Date实现Comparable接口。这意味着它具有compareTo方法,可用于比较2个日期

 int res = date1.compareTo(date2);



如果date1等于date2,则res = 0;否则,res = 0。
如果date1在date2之前,则res 如果date1在date2之后,则res> 0

10-08 18:55