即使日期相等或不相等 CompareTo 也会失败。它只打印 1。
CompareTo 在比较时不返回 0。!!
这个愚蠢的代码挤在我的头上。嘿,朋友,我在哪里犯规了?
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class date {
public static void main (String args[]) throws ParseException
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date expiry = sdf.parse("2012-11-09");
System.out.println(sdf.format(expiry));
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(expiry);
cal1.add(Calendar.DATE, -2);
System.out.println(sdf.format(cal1.getTime()));
System.out.println(sdf.format(cal2.getTime()));
int j = cal1.compareTo(cal2);
System.out.println("The result is :" + j);
}
}