Regular expressions for judging time--Regular expressions
Source: Internet
Author: User
The common method is to separate out the hours, minutes, seconds, respectively, to judge:
public static Boolean Timecheck (string time, string owner) {
Checks whether the time string times satisfies the format "HH:mm:ss" or "hh:mm" and returns False if the corresponding message is not satisfied
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 being 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 being 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 being in the [0..59] range. ";
Messagedialog.showerror (Controller.getmainframe (), MSG);
return false;
}
return true;
}
The regular method is:
public static Boolean Timecheck (string time, string owner) {
Checks whether the time string times satisfies the format "HH:mm:ss" and returns False if the corresponding message is not satisfied
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;
}
It's really good and powerful!! However, the above regular "((2[0-3)) | ( [01]\d)]: [0-5]\d (: [0-5]\d)?] Realize incredibly can't match "3:3:3", depressed!
Change the positive to "((2[0-3)) | ( [0-1]?\d)]: [0-5]?\d (: [0-5]?\d)?] Can match "3:3:3" this kind of, but "3:65:34" obviously is wrong, but again match two come, dizzy!
Change the positive to "((2[0-3)) | ( [0-1]?\d): [0-5]?\d (: [0-5]?\d)] can match "3:3:3", but also can correctly judge "3:65:34", but can't judge "3:34" this format. Oh ~ ~ ~ ~ ~
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.