编程知识 cdmana.com

[Java] check whether the current time is within the specified time

Don't talk too much and post code directly . 

My date format is 8:00-22:00

To use their own format ha .

public class CheckClosingTimeUtil {

    /**
     *  Check whether the current time is within the specified time 
     * @param businessHours  The opening hours of a store or something 
     * @return  true: At the appointed time  false: Not at the appointed time 
     */
    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;
    }

    /**
     *  Check whether the current time is within the specified time 2
     * @param businessHours  The opening hours of a store or something 
     * @param current  Company ; current time  -  millisecond 
     * @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;
        }
    }
}

 

版权声明
本文为[Thorns in ordinary days-]所创,转载请带上原文链接,感谢

Scroll to Top