try{ Date dt1 = new SimpleDateFormat("yyyy-MM-dd").parse("2004-09-11"); Date dt2 = new SimpleDateFormat("yyyy-MM-dd").parse("2005-09-11"); System.out.println(dt1.compareTo(dt2)); } catch(Exception e){} compareTo public int compareTo(Date anotherDate)Compares two Dates for ordering. Parameters: anotherDate - the Date to be compared. Returns: the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument.
刚才写的那个是两个日期的比较,至于要mydate在两个日期之间的写法,楼主自己实现吧, :)
mydate.compareTo(startdate)>0&&mydate.compareTo(enddate)<0 the value 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string.
try{ Date mydate = new SimpleDateFormat("yyyy-MM-dd").parse("200-09-09"); Date startdate = new SimpleDateFormat("yyyy-MM-dd").parse("200-04-09"); Date enddate = new SimpleDateFormat("yyyy-MM-dd").parse("200-09-09"); if (mydate.compareTo(startdate)>=0 && mydate.compareTo(enddate)<=0) System.out.println("between"); } catch(Exception e){} 控制台会输入 between
Date dt1 = new SimpleDateFormat("yyyy-MM-dd").parse("2004-09-11");
Date dt2 = new SimpleDateFormat("yyyy-MM-dd").parse("2005-09-11");
System.out.println(dt1.compareTo(dt2));
}
catch(Exception e){}
compareTo
public int compareTo(Date anotherDate)Compares two Dates for ordering. Parameters:
anotherDate - the Date to be compared.
Returns:
the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument.
the value 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string.
Date mydate = new SimpleDateFormat("yyyy-MM-dd").parse("200-09-09");
Date startdate = new SimpleDateFormat("yyyy-MM-dd").parse("200-04-09");
Date enddate = new SimpleDateFormat("yyyy-MM-dd").parse("200-09-09");
if (mydate.compareTo(startdate)>=0 && mydate.compareTo(enddate)<=0)
System.out.println("between");
}
catch(Exception e){}
控制台会输入 between