date类型的数据进行比较compareTo方法
//相等返回0,大于返回1,小于返回-1
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
//这里时间可以自己定
Date oldDate1 = format.parse("2011-05-12 15:16:00");
//这里时间可以自己定
Date oldDate2 = format.parse("2011-05-12 15:16:00");
//判断,如果时间在这时间后,就执行后面操作
System.out.println(oldDate1.compareTo(oldDate2));
} catch (ParseException e) {
e.printStackTrace();
}
底层
public int compareTo(Date anotherDate) { //转换成long类型 long thisTime = getMillisOf(this); long anotherTime = getMillisOf(anotherDate); //返回结果只有-1 0 1 return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1)); }
dug运行
结果是0