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

前端 未结 8 878
谎友^
谎友^ 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:13

    If JODA is not an option, one of the shortest approaches is probably to convert the time to strings and then compare them.

    DateFormat df = new SimpleDateFormat("HHmmssSZ");
    df.format(date1).equals(df.format(date2));
    

提交回复
热议问题