java比较两个日期 [英] java Compare two dates
本文介绍了java比较两个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想比较两个日期,并检查日期是否已过期。
I want to compare two dates and check if the date has expired or not.
以下是我使用的代码:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:ss:ii");
Date date1 = sdf.parse("20012-10-4 10:15:25");
Date date2 = sdf.parse("2013-10-4 10:15:25");
if(date1.equals(date12)){
System.out.println("Both are equals");
}
我想检查这两个日期,但没有成功。
I want to check the two dates but without success.
我还试图检查它:
if(date1 >= date2){
System.out.println("Both are not equals");
}
但它不起作用。
推荐答案
java.util.Date类具有前后方法比较日期。
java.util.Date class has before and after method to compare dates.
Date date1 = new Date();
Date date2 = new Date();
if(date1.before(date2)){
//Do Something
}
if(date1.after(date2)){
//Do Something else
}
这篇关于java比较两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文