This question already has answers here:
Find total hours between two Dates
                            
                                (11个答案)
                            
                    
                6个月前关闭。
        

    

我计算两个日期之间的时差。一个来自数据库,另一个当前时间。

Date currentDate = new Date();
Date dbDate = loader.getsuccessDate();
DateTime d1 = new DateTime (currentDate.getTime());
DateTime d2 = new DateTime (dbDate.getTime());

System.out.println(Hours.hoursBetween(d1 - d2).getHours);

// gives results as -285


如何将负结果转换为正值。

最佳答案

我认为结果是w.r.t.分钟。我会做如下。

int totalTimeInMinutes = Hours.hoursBetween(d1 - d2).getHours;
int hour = 0;
int minute = 0;
if (totalTimeInMinutes < 0)
    totalTimeInMinutes *= -1;

hour = totalTimeInMinutes / 60;
minute = totalTimeInMinutes % 60;

10-07 20:32