java 判断当前时间是否可以执行

判断时间

private boolean canTheTimeExecute() {
    try {
        if (StringUtils.isEmpty(containerStockSyncTime)) {
            log.info("[handleContainerStockSyncTask]-execute time is empty。");
            return false;
        }
        //配置中心按照分钟设置,这里转换成秒格式
        String dataTime = containerStockSyncTime + ":00";
        LocalTime time = LocalTime.parse(dataTime, DateTimeFormatter.ofPattern("HH:mm:ss"));
        String cronStr = time.format(DateTimeFormatter.ofPattern("ss mm HH * * ?"));
        if (StringUtils.isEmpty(cronStr)) {
            return false;
        }
        //获取定时器这次执行的时间
        List<Date> execTimes = CronPatternUtil.matchedDates(cronStr, DateUtil.beginOfMinute(new Date()), 1, true);
        if (CollectionUtils.isEmpty(execTimes)) {
            return false;
        }
        // 判断是否已经到了应该可以执行的时间(分钟未单位)
        return (System.currentTimeMillis() - execTimes.get(0).getTime()) >= 0;
    } catch (Exception ex) {
        log.error("[handleContainerStockSyncTask]- time parse error。", ex);
    }
    return false;
}
posted @ 2024-01-29 21:38  DaleyZou  阅读(2)  评论(0编辑  收藏  举报