我从属性文件中获取硬编码日期,其格式为 dd-MMM-yyyy
。
现在我需要将它与相同格式的当前日期进行比较。为此,我编写了这段代码:
Date convDate = new Date();
Date currentFormattedDate = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
convDate = new SimpleDateFormat("dd-MMM-yyyy").parse("20-Aug-2013");
currentFormattedDate = new Date(dateFormat.format(currentFormattedDate));
if(currentFormattedDate.after(convDate) || currentFormattedDate.equals(convDate)){
System.out.println("Correct");
}else{
System.out.println("In correct");
}
但是 eclipse 告诉我 new Date
已被折旧。有谁知道有任何替代方法可以做到这一点吗?我快要疯了。谢谢!
最佳答案
其中一种方法是使用 Calendar类及其after() , equals()和 before()方法。
Calendar currentDate = Calendar.getInstance();
Calendar anotherDate = Calendar.getInstance();
Date convDate = new SimpleDateFormat("dd-MMM-yyyy").parse("20-Aug-2013");
anotherDate.setTime(convDate);
if(currentDate .after(anotherDate) ||
currentDate .equals(anotherDate)){
System.out.println("Correct");
}else{
System.out.println("In correct");
}
<小时/>
您还可以使用Jodatime library ,参见this SO answer .
关于java - 将当前日期与属性文件中的日期进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18329122/