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

前端 未结 8 858
谎友^
谎友^ 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 17:02

    You may consider using Joda time's DateTimeComparator.getTimeOnlyInstance() for a comparator that will compare two Joda dates based only upon the times.

    For example:

    DateTimeComparator comparator = DateTimeComparator.getTimeOnlyInstance();
    comparator.compare(date1, date2);
    

    See http://joda-time.sourceforge.net/api-release/org/joda/time/DateTimeComparator.html#getTimeOnlyInstance()

提交回复
热议问题