我想計算兩個日期之間的天數差異。我的代碼在日期的年份沒有變化時工作正常,但是當我計算兩個日期之間的差異時(如2012年1月13日至2011年12月13日),它會給出負值。當我計算當前日期和未來日期之間的差異時,它也會給出錯誤的差異值。請幫幫我。先謝謝你。這裏是我的代碼:無法獲得java中兩個日期之間的天數差異
//getting values from text box
String fromtext = from.getText().toString();
String totext = to.getText().toString();
//sdf if a simple date formatter
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date fromdate = (Date) sdf.parse(fromtext);
Date todate = (Date) sdf.parse(totext);
Calendar fromcal = Calendar.getInstance();
Calendar tocal = Calendar.getInstance();
fromcal.setTime(fromdate);
tocal.setTime(todate);// setting to date
int reportDays=(int)(todate.getTime()-fromdate.getTime())/(3600*24*1000);
請告訴我什麼是計算天差的最好方法。
看看[這個例子](http://www.roseindia.net/java/beginners/DateDifferent.shtml) – CloudyMarble 2012-01-13 06:55:12