0
点赞
收藏
分享

微信扫一扫

判断任意两个时间间隔是否超过3个自然月

cnlinkchina 2022-04-14 阅读 92
java

/**
* 时间差值校验
*
* @param dates 时间数组
* @return 对比结果
*/
private Boolean checkMonth(Date… dates) {
if (dates.length > 1) {
for (int i = 0; i < dates.length; i++) {
for (int j = 1; j < dates.length; j++) {
Boolean month = getMonth(dates[i], dates[j]);
if (month) {
return Boolean.TRUE;
}
}
}
}
return Boolean.FALSE;
}

/**
 * 计算两个时间相差是否在3个月内
 *
 * @param start 开始时间
 * @param end   结束时间
 * @return 对比结果true是在3个月内,false为不在3个月内
 */
private Boolean getMonth(Date start, Date end) {
    if (start.after(end)) {
        Date t = start;
        start = end;
        end = t;
    }
    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
    c1.setTime(start);
    c2.setTime(end);
    c1.add(Calendar.MONTH, 3);
    if (c1.getTimeInMillis() <= c2.getTimeInMillis()) {
        return Boolean.FALSE;
    }
    return Boolean.TRUE;
}
举报

相关推荐

0 条评论