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 ~~~~
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.