编程知识 cdmana.com

【java】校验当前时间是否在规定的时间内

废话不多说直接贴代码。 

我的日期格式是 8:00-22:00

要用的自己换下格式哈。

public class CheckClosingTimeUtil {

    /**
     * 校验当前时间是否在规定时间内
     * @param businessHours 某商店营业时间或者是别的
     * @return  true:在规定时间 false:未在规定时间
     */
    public static boolean checkClosingTime(String businessHours){

        if(StringUtils.isNotBlank(businessHours)){
            BulidBusinessHours bulidBusinessHours = new BulidBusinessHours(businessHours).invoke();
            long start = bulidBusinessHours.getStart();
            long end = bulidBusinessHours.getEnd();
            Calendar currentTime = Calendar.getInstance();
            long current = currentTime.getTimeInMillis();
            if(current>start && current<end){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }

    /**
     * 校验当前时间是否在规定时间内2
     * @param businessHours 某商店营业时间或者是别的
     * @param current 单位;当前时间 - 毫秒级
     * @return
     */
    public static boolean checkClosingTime(String businessHours,long current){

        if(StringUtils.isNotBlank(businessHours)){
            BulidBusinessHours bulidBusinessHours = new BulidBusinessHours(businessHours).invoke();
            long start = bulidBusinessHours.getStart();
            long end = bulidBusinessHours.getEnd();
            if(current>start && current<end){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }

    private static class BulidBusinessHours {
        private String businessHours;
        private long start;
        private long end;

        public BulidBusinessHours(String businessHours) {
            this.businessHours = businessHours;
        }

        public long getStart() {
            return start;
        }

        public long getEnd() {
            return end;
        }

        public BulidBusinessHours invoke() {
            List<String> businessTime = Arrays.asList(businessHours.split("-"));
            List<String> startBusinessTime = Arrays.asList(businessTime.get(0).split(":"));
            List<Integer> startTime = startBusinessTime.stream().map(e -> Integer.valueOf(e)).collect(Collectors.toList());
            List<String> endBusinessTime = Arrays.asList(businessTime.get(1).split(":"));
            List<Integer> endTime = endBusinessTime.stream().map(e -> Integer.valueOf(e)).collect(Collectors.toList());

            Calendar drugstoreStartTime = Calendar.getInstance();
            drugstoreStartTime.set(Calendar.HOUR_OF_DAY, startTime.get(0));
            drugstoreStartTime.set(Calendar.MINUTE, startTime.get(1));
            drugstoreStartTime.set(Calendar.SECOND, 0);
            start = drugstoreStartTime.getTimeInMillis();

            Calendar drugstoreEndTime = Calendar.getInstance();
            drugstoreEndTime.set(Calendar.HOUR_OF_DAY, endTime.get(0));
            drugstoreEndTime.set(Calendar.MINUTE, endTime.get(1));
            drugstoreEndTime.set(Calendar.SECOND, 0);
            end = drugstoreEndTime.getTimeInMillis();
            return this;
        }
    }
}

 

版权声明
本文为[平淡日子里的刺-]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/xiaowangshua/p/13966243.html

Scroll to Top