比较忽略毫秒的日期? [英] Compare dates ignoring milliseconds?

查看:169
本文介绍了比较忽略毫秒的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法比较两个日历对象,但忽略毫秒?

我编写了一个比较两个日历对象的测试用例, 但有个问题。虽然所有日,月,分和小时都匹配,但毫秒不匹配。我在获得实际日期之前得到预期日期:

I have written a test case that compared two calendar objects, but there is a problem. Although all of the day, month, minutes and hours match, the milliseconds doesn't matches. I get the expected date before getting the real date:

/**
 * @return
 */
private Calendar getExpectedOneMonthDateFromCurrentDate() {
    Calendar expectedOneMonth = Calendar.getInstance();
    expectedOneMonth.add(Calendar.MONTH, -1);
    return expectedOneMonth;
}

assertEquals(getExpectedOneMonthDateFromCurrentDate(),
             DateRange.LAST_ONE_MONTH.getToDate());


推荐答案

从日历中删除毫秒

cal.set(Calendar.MILLISECOND, 0);

这篇关于比较忽略毫秒的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆