主頁 > 知識庫 > 判斷時(shí)間的正則表達(dá)式

判斷時(shí)間的正則表達(dá)式

熱門標(biāo)簽:高德地圖標(biāo)注短信簽約 百音電話機(jī)器人 福州工作銷售電話機(jī)器人 自制電銷機(jī)器人 錫林郭勒盟地圖標(biāo)注位置 知名的電話機(jī)器人 RO地圖標(biāo)注app 電銷機(jī)器人公司簡介 湖南企業(yè)智能外呼系統(tǒng)供應(yīng)商
普通方法為,分離出小時(shí)、分鐘、秒分別進(jìn)行判斷:
public static boolean timeCheck(String time, String owner) {
//檢查時(shí)間字符串time是否滿足格式“HH:mm:ss”或“HH:mm”,若不滿足顯示相應(yīng)消息,并返回false
if(time.equals("")){
String msg = owner+" : "+"Time is EMPTY.";
MessageDialog.showError(Controller.getMainFrame(), msg);
return false;
}
int hours, minutes, seconds = 0;
StringTokenizer st = new StringTokenizer(time, ":");
int tokens = st.countTokens();
if (tokens != 3 tokens != 2) {
String msg = owner+" : "+"Time " + time + " does not conform to the HH:mm:ss format, nor HH:mm format.";
MessageDialog.showError(Controller.getMainFrame(), msg);
return false;
}
String hourToken = st.nextToken();
try {
hours = Integer.parseInt(hourToken);
} catch (NumberFormatException nfe) {
String msg = owner+" : "+hourToken + " in " + time + " can not be parsed as hours.";
MessageDialog.showError(Controller.getMainFrame(), msg);
return false;
}
String minuteToken = st.nextToken();
try {
minutes = Integer.parseInt(minuteToken);
} catch (NumberFormatException nfe) {
String msg = owner+" : "+minuteToken + " in " + time + " can not be parsed as minutes.";
MessageDialog.showError(Controller.getMainFrame(), msg);
return false;
}
if(tokens == 3){
String secondToken = st.nextToken();
try {
seconds = Integer.parseInt(secondToken);
} catch (NumberFormatException nfe) {
String msg = owner+" : "+secondToken + " in " + time + " can not be parsed as seconds.";
MessageDialog.showError(Controller.getMainFrame(), msg);
return false;
}
}
if (hours 0 || hours > 23) {
String msg = owner+" : "+"Specified hours: " + hours + ". Number of hours must be in the [0..23] range.";
MessageDialog.showError(Controller.getMainFrame(), msg);
return false;
}
if (minutes 0 || minutes > 59) {
String msg = owner+" : "+"Specified minutes: " + minutes + ". Number of minutes must be in the [0..59] range.";
MessageDialog.showError(Controller.getMainFrame(), msg);
return false;
}
if (seconds 0 || seconds > 59) {
String msg = owner+" : "+"Specified seconds: " + seconds + ". Number of seconds must be in the [0..59] range.";
MessageDialog.showError(Controller.getMainFrame(), msg);
return false;
}
return true;
}
正則方法為:
public static boolean timeCheck(String time, String owner) {
//檢查時(shí)間字符串time是否滿足格式“HH:mm:ss”,若不滿足顯示相應(yīng)消息,并返回false
String regex = "(([01]\\d)|(2[0-3])):[0-5]\\d(:[0-5]\\d)?";
if(!time.matches(regex)){
String msg = owner+" : "+"Time is INVALID.";
MessageDialog.showError(Controller.getMainFrame(), msg);
return false;
}
return true;
}
  真是很好很強(qiáng)大?。。〔贿^上述正則“((2[0-3])|([01]\d)):[0-5]\d(:[0-5]\d)?”實(shí)現(xiàn)居然不能匹配“3:3:3”,郁悶!
  將正則改為“((2[0-3])|([0-1]?\d)):[0-5]?\d(:[0-5]?\d)?”倒是可以匹配“3:3:3”這種了,但“3:65:34”顯然是不對的,卻又匹配出兩個(gè)來,暈!
  將正則改為“((2[0-3])|([0-1]?\d)):[0-5]?\d(:[0-5]?\d)”倒是可以匹配“3:3:3”,也能正確判斷“3:65:34”,卻又沒法判斷“3:34”這種格式了。唉~~~~

標(biāo)簽:河北 吉林 秦皇島 玉林 怒江 昆明 茂名 西寧

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《判斷時(shí)間的正則表達(dá)式》,本文關(guān)鍵詞  判斷,時(shí),間的,正則,表達(dá)式,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《判斷時(shí)間的正則表達(dá)式》相關(guān)的同類信息!
  • 本頁收集關(guān)于判斷時(shí)間的正則表達(dá)式的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章