The common method is to separate the hours, minutes, And seconds for determination:
Public static boolean timeCheck (String time, String owner ){
// Check whether the time string meets the format of "HH: mm: ss" or "HH: mm". If the format is not met, the system returns 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;
}
The regular expression method is as follows:
Public static boolean timeCheck (String time, String owner ){
// Check whether the time string meets the format of "HH: mm: ss". If not, the corresponding message is displayed and false is returned.
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;
}
Very good and powerful !! However, the above regular "(2 [0-3]) | ([01] \ d): [0-5] \ d (: [0-5] \ d )?" The implementation cannot match "3: 3". It is depressing!
Change the regular expression to "(2 [0-3]) | ([0-1]? \ D): [0-5]? \ D (: [0-5]? \ D )?" It can match "", but "3: 65: 34" is obviously incorrect, but it matches two, dizzy!
Change the regular expression to "(2 [0-3]) | ([0-1]? \ D): [0-5]? \ D (: [0-5]? \ D) "can match" ", and can correctly judge" 3: 65: 34 ", but cannot judge. Alas ~~~~