我想比较两个日期,看看是不是过期了。
下面是我使用的代码:
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");
}我想检查两个日期,但没有成功。
我也试着这样检查它:
if(date1 >= date2){
System.out.println("Both are not equals");
}但它也不起作用。
发布于 2013-04-10 20:17:19
java.util.Date类具有用于比较日期的之前和之后方法。
Date date1 = new Date();
Date date2 = new Date();
if(date1.before(date2)){
//Do Something
}
if(date1.after(date2)){
//Do Something else
}发布于 2013-04-10 20:25:28
尝试使用此Function.It将帮助您:-
public class Main {
public static void main(String args[])
{
Date today=new Date();
Date myDate=new Date(today.getYear(),today.getMonth()-1,today.getDay());
System.out.println("My Date is"+myDate);
System.out.println("Today Date is"+today);
if(today.compareTo(myDate)<0)
System.out.println("Today Date is Lesser than my Date");
else if(today.compareTo(myDate)>0)
System.out.println("Today Date is Greater than my date");
else
System.out.println("Both Dates are equal");
}
}发布于 2013-04-10 20:15:57
阅读JavaDocs。
使用方法:
Date.compareTo()https://stackoverflow.com/questions/15925509
复制相似问题