JAVA 时间区间的字符串合法性验证

复制代码 代码如下:

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();

        }

0.225469s