Home  >  Article  >  Java  >  JAVA time interval string validity verification

JAVA time interval string validity verification

巴扎黑
巴扎黑Original
2017-05-21 14:27:541552browse

It is necessary to verify the legality of the obtained strings such as 08:30-11:00, determine whether the time interval represented is legal, and determine whether the peak time interval is within the total time interval.

The code is as follows:

String[] zone1="08:30-11:00".split("-");
        String[] zone2="13:00-17:00".split("-");
        String[] actzone="9:00-11:00".split("-");
        DateFormat df = new SimpleDateFormat("hh:mm");
        try{
        long mintime=df.parse("00:00").getTime();
        long maxtime=df.parse("24:00").getTime();
        long[] time1={df.parse(zone1[0]).getTime(),df.parse(zone1[1]).getTime()};
        long[] time2={df.parse(zone2[0]).getTime(),df.parse(zone2[1]).getTime()};
        long[] acttime={df.parse(actzone[0]).getTime(),df.parse(actzone[1]).getTime()};
        if(time1[0]>=time1[1]||time1[0]<=mintime||time1[1]>=maxtime)
            System.out.println("操作区间一不合法");
        if(time2[0]>=time2[1]||time2[0]<=mintime||time2[1]>=maxtime)
            System.out.println("操作区间二不合法");
        if(acttime[0]>=acttime[1]||acttime[0]<=mintime||acttime[1]>=maxtime)
            System.out.println("高峰期区间不合法");
        boolean ifLegal=false;
        if(acttime[0]>=time1[0]&&acttime[1]<=time1[1])
            ifLegal=true;
        if(acttime[0]>=time2[0]&&acttime[1]<=time2[1])
            ifLegal=true;
        if(ifLegal)
            System.out.println("info:高峰期区间合法");
        else
            System.out.println("warn:高峰期区间不合法");
        }catch(Exception e){
            e.printStackTrace();
        }

The above is the detailed content of JAVA time interval string validity verification. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn