Compare only the time portion of two dates, ignoring the date part

前端 未结 8 816
谎友^
谎友^ 2021-01-07 16:34

What\'s a good method to, given two Date objects, compare the difference between their time portion only, completely ignoring Year, Month and Day?

It\'s quite the op

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-07 16:54

    Take a look at the Calendar class. It has support for extracting hours, minutes, and seconds from given Date's.

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(yourDateObject);
    int hour = calendar.get(Calendar.HOUR);
    int minute = calendar.get(Calendar.MINUTE);
    int second = calendar.get(Calendar.SECOND);
    

提交回复
热议问题