See if the current time falls within a specific range of time in the current day in Java

前端 未结 6 869
天命终不由人
天命终不由人 2020-12-10 13:52

I am sure this was done 1000 times in 1000 different places. The question is I want to know if there is a better/standard/faster way to check if current \"time\" is between

6条回答
  •  [愿得一人]
    2020-12-10 14:49

    The following Class is something I just created out of some of the code from other answers. It encapsulates the behavior of a 'time period' without relating to specific days. Our system is using this Class to check if the current time is within one of our designated maintenance windows. i.e. 05:00:00 - 07:00:00

    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    
    /**
    *
    * @author Adam Yocum
    */
    public class ExclusionTimePeriod {
        private String timeStart;
        private String timeEnd;
    
        /**
        * @return the timeStart
        */
        public String getTimeStart() {
            return timeStart;
        }
    
        /**
        * @param timeStart the timeStart to set
        */
        public void setTimeStart(String timeStart) {
            if (timeStart.matches("^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$"))
            {
                this.timeStart = timeStart;
            }
            else
            {
                throw new IllegalArgumentException(timeStart + " is not a valid time, expecting HH:MM:SS format");
            }
    
        }
    
        /**
        * @return the timeEnd
        */
        public String getTimeEnd() {
            return timeEnd;
        }
    
        /**
        * @param timeEnd the timeEnd to set
        */
        public void setTimeEnd(String timeEnd) {
            if (timeEnd.matches("^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$"))
            {
                this.timeEnd = timeEnd;
            }
            else
            {
                throw new IllegalArgumentException(timeEnd + " is not a valid time, expecting HH:MM:SS format");
            }
        }
    
        private Date toDate(String hhmmss){
            final String[] hms = hhmmss.split(":");
            final GregorianCalendar gc = new GregorianCalendar();
            gc.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hms[0]));
            gc.set(Calendar.MINUTE, Integer.parseInt(hms[1]));
            gc.set(Calendar.SECOND, Integer.parseInt(hms[2]));
            gc.set(Calendar.MILLISECOND, 0);
            Date date = gc.getTime();
            return date;
        }
    
        public boolean isNowInPeriod()
        {
            final Date now = new Date();
            return now.after(toDate(getTimeStart())) && now.before(toDate(getTimeEnd()));
        }
    
        public static void main(String[] args){
    
            //Test All possible hours
            for(int hour=0;hour<=23;hour++){
    
                String hourStr = "";
                if(hour<=9){
                    hourStr = "0"+hour;
                }else{
                    hourStr = ""+hour;
                }
    
                for(int min=0;min<60;min++){
                    String minStr = "";
                    if(min<=9){
                        minStr = "0"+min;
                    }else{
                        minStr = ""+min;
                    }
    
                    for(int sec=0;sec<60;sec++){
                        String secStr = "";
                        if(sec<=9){
                            secStr = "0"+sec;
                        }else{
                            secStr = ""+sec;
                        }
    
                        String hhmmss = hourStr+":"+minStr+":"+secStr;
    
                        ExclusionTimePeriod period = new ExclusionTimePeriod();
                        period.setTimeStart(hhmmss);
                        period.setTimeEnd(hhmmss);
    
                        System.out.println(hhmmss+" Ok");
                    }
                }
            }
    
    
            //Test isInPeriod functionality
            ExclusionTimePeriod isInTest = new ExclusionTimePeriod();
            isInTest.setTimeStart("10:00:00");
            isInTest.setTimeEnd("10:43:00");
    
            System.out.println((new Date())+" is between "+isInTest.getTimeStart()+" and "+isInTest.getTimeEnd()+" = "+isInTest.isNowInPeriod());
    
        }
    }
    

提交回复
热议问题