Find total hours between two Dates

前端 未结 11 1341
抹茶落季
抹茶落季 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:48

    Here's simple way:

    private static int hoursDifference(Date date1, Date date2) {
    
        final int MILLI_TO_HOUR = 1000 * 60 * 60;
        return (int) (date1.getTime() - date2.getTime()) / MILLI_TO_HOUR;
    }
    

提交回复
热议问题