JAVA 時間區間的字串合法性驗證

來源:互聯網
上載者:User

複製代碼 代碼如下: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();
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.