【java】校驗當前時間是否在規定的時間內

發表於2020-11-12

廢話不多說直接貼程式碼。 

我的日期格式是 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;
        }
    }
}

 

相關文章