使用Java进行日期比较

时间:2010-05-11 13:36:39

标签: java date

我有两个日期:

  1. toDateMM/dd/yyyy格式的用户输入)
  2. currentDate(由new Date()获得)
  3. 我需要将currentDatetoDate进行比较。我必须仅在toDate等于或大于currentDate时才显示报告。我怎么能这样做?

9 个答案:

答案 0 :(得分:18)

使用java.util.Calendar比较日期会更容易。 您可以这样做:

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
}

答案 1 :(得分:11)

如果你开始使用Java Dates而不是JodaTime,请使用java.text.DateFormat将字符串转换为Date,然后使用.equals比较两者:

我差点忘了:在比较它们之前,您需要将当前日期的小时,分​​钟,秒和毫秒归零。我使用下面的Calendar对象来完成它。

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

// Other code here
    String toDate;
    //toDate = "05/11/2010";

    // Value assigned to toDate somewhere in here

    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
    Calendar currDtCal = Calendar.getInstance();

    // Zero out the hour, minute, second, and millisecond
    currDtCal.set(Calendar.HOUR_OF_DAY, 0);
    currDtCal.set(Calendar.MINUTE, 0);
    currDtCal.set(Calendar.SECOND, 0);
    currDtCal.set(Calendar.MILLISECOND, 0);

    Date currDt = currDtCal.getTime();

    Date toDt;
    try {
        toDt = df.parse(toDate);
    } catch (ParseException e) {
        toDt = null;
        // Print some error message back to the user
    }

    if (currDt.equals(toDt)) {
        // They're the same date
    }

答案 2 :(得分:6)

Date#equals()Date#after()

如果hourminute字段有可能!= 0,则必须将它们设置为0.

我不能忘记提到使用java.util.Date被认为是一种不好的做法,并且大多数方法都被弃用了。如果可能,请使用java.util.CalendarJodaTime

答案 3 :(得分:5)

您可能正在寻找:

!toDate.before(currentDate)

before()和after()测试日期是严格之前还是之后。所以你必须取消另一个的否定才能得到非严格的行为。

答案 4 :(得分:4)

这是其中一种方式:

String toDate = "05/11/2010";

if (new SimpleDateFormat("MM/dd/yyyy").parse(toDate).getTime() / (1000 * 60 * 60 * 24) >= System.currentTimeMillis() / (1000 * 60 * 60 * 24)) {
    System.out.println("Display report.");
} else {
    System.out.println("Don't display report.");
}

更容易解释:

String toDateAsString = "05/11/2010";
Date toDate = new SimpleDateFormat("MM/dd/yyyy").parse(toDateAsString);
long toDateAsTimestamp = toDate.getTime();
long currentTimestamp = System.currentTimeMillis();
long getRidOfTime = 1000 * 60 * 60 * 24;
long toDateAsTimestampWithoutTime = toDateAsTimestamp / getRidOfTime;
long currentTimestampWithoutTime = currentTimestamp / getRidOfTime;

if (toDateAsTimestampWithoutTime >= currentTimestampWithoutTime) {
    System.out.println("Display report.");
} else {
    System.out.println("Don't display report.");
}

哦,作为奖励,JodaTime的变种:

String toDateAsString = "05/11/2010";
DateTime toDate = DateTimeFormat.forPattern("MM/dd/yyyy").parseDateTime(toDateAsString);
DateTime now = new DateTime();

if (!toDate.toLocalDate().isBefore(now.toLocalDate())) {
    System.out.println("Display report.");
} else {
    System.out.println("Don't display report.");
}

答案 5 :(得分:3)

日期long getTime()返回此Date对象表示的January 1, 1970, 00:00:00 GMT以来的毫秒数。

//test if date1 is before date2
if(date1.getTime() < date2.getTime()) {
....
}

答案 6 :(得分:2)

private boolean checkDateLimit(){

    long CurrentDateInMilisecond = System.currentTimeMillis(); // Date 1
    long Date1InMilisecond = Date1.getTimeInMillis(); //Date2

    if (CurrentDateInMilisecond <= Date1InMilisecond) {
        return true;
    } else {
        return false;
    }

}

//将两个日期转换为毫秒值。

答案 7 :(得分:1)

如果出于某种原因,您打算在解决方案中使用Date个对象,则需要执行以下操作:


    // Convert user input into year, month, and day integers
    Date toDate = new Date(year - 1900, month - 1, day + 1);
    Date currentDate = new Date();
    boolean runThatReport = toDate.after(currentDate);

toDate向前移动到第二天的午夜将照顾我在其他答案的评论中抱怨的错误。但请注意,此方法使用不推荐使用的构造函数;任何依赖于Date的方法都会使用一种弃用的方法或者另一种方法,并且取决于你的方法,也可能导致竞争条件(如果你基于toDate new Date(),然后小提琴例如,年,月,日。使用Calendar,如其他地方所述。

答案 8 :(得分:0)

如果您有广泛的日期相关处理,请使用java.util.Calendar

日期有before()after()方法。你也可以使用它们。