在java.util.Date类中有一个方法public int compareTo(Date anotherDate) 应该可以解决你的问题。 试一下吧。
Date date1 = new Date(1061521133802L); System.out.println(date1); Date date2 = new Date(1001521133802L); System.out.println(date2); Calendar cal = Calendar.getInstance(); Date sub = new Date(date1.getTime() - date2.getTime()); cal.setTime(sub); System.out.print("相差" + (cal.get(Calendar.YEAR) - 1970) + "年"); System.out.print((cal.get(Calendar.MONTH) + 1) + "月"); System.out.println(cal.get(Calendar.DAY_OF_MONTH) + "天");
//计算两个日期之间相隔的天数 public int getDayInRange(Date lowerLimitDate,Date upperLimitDate) throws InsufficientDateException{
long upperTime,lowerTime; upperTime=upperLimitDate.getTime(); lowerTime=lowerLimitDate.getTime(); if(upperTime<lowerTime) throw new InsufficientDateException(); Long result=new Long((upperTime-lowerTime)/(1000*60*60*24)); return result.intValue(); }
应该可以解决你的问题。
试一下吧。
System.out.println(date1);
Date date2 = new Date(1001521133802L);
System.out.println(date2);
Calendar cal = Calendar.getInstance();
Date sub = new Date(date1.getTime() - date2.getTime());
cal.setTime(sub);
System.out.print("相差" + (cal.get(Calendar.YEAR) - 1970) + "年");
System.out.print((cal.get(Calendar.MONTH) + 1) + "月");
System.out.println(cal.get(Calendar.DAY_OF_MONTH) + "天");
public int getDayInRange(Date lowerLimitDate,Date upperLimitDate)
throws InsufficientDateException{
long upperTime,lowerTime;
upperTime=upperLimitDate.getTime();
lowerTime=lowerLimitDate.getTime();
if(upperTime<lowerTime)
throw new InsufficientDateException();
Long result=new Long((upperTime-lowerTime)/(1000*60*60*24));
return result.intValue();
}