Java Date compareTo()方法始终返回-1

Java Date compareTo()方法始终返回-1,java,android,Java,Android,我有两个约会,我想比较一下。我已经记录了实际日期,以确保其正确无误 Date photoDate = new Date(mPhotoObject.calendar.getTimeInMillis()); SimpleDateFormat dateFormat = new SimpleDateFormat("M.d.yy"); Log.v("photo date is", dateFormat.format(photoDate)); Date currentDate = new Date(C

我有两个约会,我想比较一下。我已经记录了实际日期,以确保其正确无误

Date photoDate = new Date(mPhotoObject.calendar.getTimeInMillis());

SimpleDateFormat dateFormat = new SimpleDateFormat("M.d.yy");


Log.v("photo date is", dateFormat.format(photoDate));
Date currentDate = new Date(Calendar.getInstance().getTimeInMillis());
Log.v("current date is", dateFormat.format(currentDate));
Log.v("date comparison", photoDate.compareTo(currentDate)+"");

if(photoDate.compareTo(currentDate)<0) {
     view.showFooButton(false);
  } else {
     view.showFooButton(true);
  }
datephotodate=新日期(mPhotoObject.calendar.getTimeInMillis());
SimpleDataFormat dateFormat=新的SimpleDataFormat(“M.d.yy”);
Log.v(“照片日期为”,dateFormat.format(photoDate));
Date currentDate=新日期(Calendar.getInstance().getTimeInMillis());
Log.v(“当前日期为”,dateFormat.format(currentDate));
Log.v(“日期比较”,photoDate.compareTo(currentDate)+”);

如果(photoDate.compareTo(currentDate)这是预期的行为,那么如果参数在日期之后,它将返回-1


这是预期的行为,如果参数在日期之后,则返回-1


日期
包含的时间最短为毫秒。您需要使用不同的比较器或修剪时间信息:

final long millisPerDay= 24 * 60 * 60 * 1000;
...
Date photoDate = new Date((long)Math.floor(mPhotoObject.calendar.getTimeInMillis() / millisPerDay) * millisPerDay);
...
Date currentDate = new Date((long)Math.floor(Calendar.getInstance().getTimeInMillis() / millisPerDay) * millisPerDay);

Date
包含的时间最短为毫秒。您需要使用不同的比较器或修剪时间信息:

final long millisPerDay= 24 * 60 * 60 * 1000;
...
Date photoDate = new Date((long)Math.floor(mPhotoObject.calendar.getTimeInMillis() / millisPerDay) * millisPerDay);
...
Date currentDate = new Date((long)Math.floor(Calendar.getInstance().getTimeInMillis() / millisPerDay) * millisPerDay);

另一个解决方案是,由于您只希望比较日、月和年,因此应该创建另一个日期的克隆,并根据需要设置日、月、年

Date date=new Date(otherDate.getTime());
date.setDate(...);
date.setMonth(...);
date.setYear(...);
然后使用比较

仅使用日期、月份和年份比较两个日期的示例函数为:

public static int compareDatesOnly(final Date date1, final Date date2) {
    final Date dateToCompare = new Date(date1.getTime());
    dateToCompare.setDate(date2.getDate());
    dateToCompare.setMonth(date2.getMonth());
    dateToCompare.setYear(date2.getYear());
    return date1.compareTo(dateToCompare);
}

另一个解决方案是,由于您只希望比较日、月和年,因此应该创建另一个日期的克隆,并根据需要设置日、月、年

Date date=new Date(otherDate.getTime());
date.setDate(...);
date.setMonth(...);
date.setYear(...);
然后使用比较

仅使用日期、月份和年份比较两个日期的示例函数为:

public static int compareDatesOnly(final Date date1, final Date date2) {
    final Date dateToCompare = new Date(date1.getTime());
    dateToCompare.setDate(date2.getDate());
    dateToCompare.setMonth(date2.getMonth());
    dateToCompare.setYear(date2.getYear());
    return date1.compareTo(dateToCompare);
}

对不起,我的意思是相反的。;)我已经编辑了我的问题。对不起,我的意思是相反的。;)我已编辑了我的问题。另请参见:另请参见:谢谢,弗拉德。我很感激。我试过了,但似乎不起作用。可能是因为我必须将它转换为int(即使它被声明为int)@LuxuryMode:实际上它需要转换为
long
,因为Math.floor()返回double。更新了,很有魅力。谢谢你,好心的先生。谢谢你,弗拉德。我很感激。我试过了,但似乎不起作用。可能是因为我必须将它转换为int(即使它被声明为int)@LuxuryMode:实际上它需要转换为
long
,因为Math.floor()返回double。更新了,很有魅力。谢谢你,好心的先生。