日期比较不起作用

Date comparison not working

我试图通过减去然后将毫秒除以天来比较两个日期,但是这个 returns 每次都是 -5479。我的语法有问题吗?我不知道为什么会这样。

if (task_date_view != null) {
    Date current_date = new Date();
    String myFormat = "MM/dd/yy";
    DateFormat dateFormat = new SimpleDateFormat(myFormat);
    Date temp_date;
    try {
        temp_date = dateFormat.parse(list_task.getDate());
        long difference = temp_date.getTime() - current_date.getTime();
        long diffDays = difference / (24 * 60 * 60 * 1000);
        String date_string = Long.toString(diffDays);
        task_date_view.setText(date_string + " days left.");

    } catch (ParseException e) {
        task_date_view.setText("No days left.");
    }

}

我认为如果您与过去的日期进行比较(例如,许可证剩余时间),您很可能会得到负数,因为这是倒退的:

temp_date.getTime() - current_date.getTime()

如何获得天数差异:

long end = endDate.getTime();
long start = startDate.getTime();
int daysDiff = TimeUnit.MILLISECONDS.toDays(Math.abs(end - start));

如果您想要与现在不同的日期,请使用:

long start = System.currentTimeInMillis();