Find total hours between two Dates

前端 未结 11 1356
抹茶落季
抹茶落季 2020-11-28 06:00

I have two Date objects and I need to get the time difference so I can determine the total hours between them. They happen to be from the same day. The result I would like w

11条回答
  •  清歌不尽
    2020-11-28 06:51

    Even though there's already an accepted answer, this is what worked for me using the Joda time library.

    /**
     *
     * @param date1
     * @param date2
     * @return hours between two dates rounded down
     */
    public static int hoursBetween(DateTime date1, DateTime date2) {
        if(date1 == null || date2 == null) return NOT_FOUND;
    
        return Math.abs(Hours.hoursBetween(date1.toLocalDateTime(), date2.toLocalDateTime()).getHours());
    }
    

提交回复
热议问题