使用Java [复制]的日期比较

    这个问题在这里已有答案:                           如何比较Java中的日期? [重复]                                      11个答案                                    
已邀请:
使用
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
}
    
如果您开始使用Java日期而不是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
    }
    
Date#equals()
Date#after()
如果
hour
minute
字段有可能!= 0,则必须将它们设置为0。 我不能忘记提到使用
java.util.Date
被认为是一种不好的做法,并且大多数方法都被弃用了。如果可能,请使用
java.util.Calendar
或JodaTime。     
您可能正在寻找:
!toDate.before(currentDate)
before()和after()测试日期是严格之前还是之后。所以你必须取消另一个的否定来获得非严格的行为。     
这是方法之一:
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.");
}
    
日期
long getTime()
返回自此Date对象表示的
January 1, 1970, 00:00:00 GMT
以来的毫秒数。
//test if date1 is before date2
if(date1.getTime() < date2.getTime()) {
....
}
    
private boolean checkDateLimit(){
    long CurrentDateInMilisecond = System.currentTimeMillis(); // Date 1
    long Date1InMilisecond = Date1.getTimeInMillis(); //Date2

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

}
//将两个日期转换为毫秒值。     
如果由于某种原因你打算在解决方案中使用
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
。     
如果您有广泛的日期相关处理,请使用
java.util.Calendar
。 日期有
before()
after()
方法。你也可以使用它们。     

要回复问题请先登录注册