使用Java进行日期比较

min*_*nil 31 java date

我有两个约会:

  1. toDate(用户输入MM/dd/yyyy格式)
  2. currentDate(获得new Date())

我需要比较currentDatetoDate.我只有在toDate等于或大于时才显示报告currentDate.我怎样才能做到这一点?

Abd*_*oof 18

使用the比较日期比较容易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
}
Run Code Online (Sandbox Code Playgroud)

  • +1 用于使用日历。如果您不是在接近毫秒级别进行比较,您通常希望在创建日历后立即 clear() 日历,然后调用 set 方法。 (2认同)

Pow*_*ord 11

如果你开始使用Java Dates而不是JodaTime,请使用a 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
    }
Run Code Online (Sandbox Code Playgroud)


Boz*_*zho 6

Date#equals()Date#after()

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

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


tkr*_*tkr 5

您可能正在寻找:

!toDate.before(currentDate)
Run Code Online (Sandbox Code Playgroud)

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


Bal*_*usC 5

这是其中一种方法:

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.");
}
Run Code Online (Sandbox Code Playgroud)

更容易理解一点:

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.");
}
Run Code Online (Sandbox Code Playgroud)

哦,作为奖励,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.");
}
Run Code Online (Sandbox Code Playgroud)