java - 将当前日期与属性文件中的日期进行比较

标签 java date

我从属性文件中获取硬编码日期,其格式为 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/

相关文章:

java - HTTPS 主机名错误 : should be <sub. domain.com>。这是什么原因造成的?

java - IllegalArgumentException : Parse error java. util.Date

python - 根据 pandas 中的日期范围创建 FY

php - 将 TIMESTAMP 和时区转换为不同的时区

python - 使用 dateutil.parse 解析不完整的日期

java - 扫描 PDF 并转换为缓冲图像以解码 QR 时出现 Zxing 格式异常

java - 调用 executorService.invokeAll(callables) 时如何按顺序获取 Future 列表?

JavaMelody 和 Spring

javascript - 从 JavaScript 日期中删除 "(Eastern Standard Time)"

Java:LDAP 搜索返回 1 行