使用Java的日期比较 [英] Date Comparison using Java

查看:96
本文介绍了使用Java的日期比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个日期:


  1. toDate (用户输入 MM / dd / yyyy 格式)

  2. currentDate (由$ code> new Date())

  1. toDate (user input in MM/dd/yyyy format)
  2. currentDate (obtained by new Date())

我需要比较 currentDate toDate 。只有当 toDate 等于或大于 currentDate 时,才显示报告。如何做到这一点?

I need to compare the currentDate with toDate. I have to display a report only when the toDate is equal to or more than currentDate. How can I do that?

推荐答案

使用 java.util.Calendar
这是你可能会做的:

It is easier to compare dates using the java.util.Calendar. Here is what you might do:

Calendar toDate = Calendar.getInstance();
Calendar nowDate = Calendar.getInstance();
toDate.set(<set-year>,<set-month>,<set-day>);  
if(!toDate.before(nowDate)) {
    //display your report
} else {
    // don't display the report
}

这篇关于使用Java的日期比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆