This question already has answers here:
How do I get difference between two dates in android?, tried every thing and post
                                
                                    (8个答案)
                                
                        
                                去年关闭。
            
                    
如何获得两个日期(包括时间)之间的天数。例如从今天晚上8点到另一天晚上8点之间的天数?我正在使用它,但它根据每月的天数

public static int getDaysTillOmer(Date d1, Date d2) {
    int daysdiff = 0;
    long diff = d2.getTime() - d1.getTime();
    long diffDays = diff / (24 * 60 * 60 * 1000);
    daysdiff = (int) diffDays;

    return daysdiff;
}

最佳答案

使用Joda Time做到这一点。

public void calcDays(Date start,Date end) {

        Duration d = new Duration(start.getTime(), end.getTime());


        System.out.println("Number of days from dates "+d.getStandardDays());
}


Duration是JodaTime的类

08-25 14:20
查看更多