Date日期类型比较

    xiaoxiao2021-04-11  45

    在java中经常会用到两个Date类型变量之间的比较,那个日期在那个日期之前或之后,这里采用Date的after方法来实现,参考如下代码

     

    /** * 日期比较 * @param cancleDate 日期1 * @param currentDate 日期2 * @return true cancleDate在currentDate之前; false cancleDate在currentDate之后 * @throws Exception */ private boolean datecompare(Date cancleDate,Date currentDate) throws Exception{ boolean flag = currentDate.after(cancleDate); if(flag){ //cancleDate在currentDate之前 return true; }else{ //cancleDate在currentDate之后 return false; } }

    运行一下,查看结果

    可以看到 currentDate是当前日期 2017-04-13 而beforeDate 是另外一个日期 2017-03-13 两者通过方法datacompare进行比较 得出的结论是

    beforeDate在currentDate之前 为正确

    currentDate在beforeDate之前 为错误

     

    转载请注明原文地址: https://ju.6miu.com/read-666918.html

    最新回复(0)