java判断两个日期是否连续

使用java判断两个日期的天是否连续

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author lingkang
 * Created by 2022/5/20
 */
public class DateUtils {

    private static final SimpleDateFormat formatDay = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 判断两个日期是否连续,单位天
     */
    public static boolean isContinuation(Date d1, Date d2) {
        try {
            Date parse1 = formatDay.parse(formatDay.format(d1));
            Date parse2 = formatDay.parse(formatDay.format(d2));
            return Math.abs(parse1.getTime() - parse2.getTime()) <= 24 * 60 * 60 * 1000;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void main(String[] args) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        boolean continuation = isContinuation(format.parse("2022-05-20 10:00:00"), format.parse("2022-05-19 10:00:00"));
        System.out.println(continuation);
    }
}
posted @ 2022-09-16 00:08  凌康  阅读(415)  评论(0编辑  收藏  举报