如何使⽤正则表达式验证24⼩时格式的时间24⼩时制正则表达式模式中的时间
([01]?[0-9]|2[0-3]):[0-5][0-9]
描述
( #start of group #1
[01]?[0-9] # start with 0-9,1-9,00-09,10-19
| # or
2[0-3] # start with 20-23
) #end of group #1
: # follow by a mi colon (:)
[0-5][0-9] # follw by 0..5 and 0..9, which means 00 to 59
24⼩时制的时钟格式是从0-23或00-23开始,然后是半冒号(:),然后是00-59。
Java正则表达式⽰例
package ;
import Matcher;
import Pattern;
public class Time24HoursValidator{
private Pattern pattern;
private Matcher matcher;
private static final String TIME24HOURS_PATTERN =
"([01]?[0-9]|2[0-3]):[0-5][0-9]";
public Time24HoursValidator(){
pattern = pile(TIME24HOURS_PATTERN);
}
/**
* Validate time in 24 hours format with regular expression
* @param time time address for validation
* @return true valid time fromat, fal invalid time format
*/
public boolean validate(final String time){
matcher = pattern.matcher(time);
return matcher.matches();
}
}
符合的时间格式:
1.“ 01:00”,“ 02:00”,“ 13:00”,
2.“ 1:00”,“ 2:00”,“ 13:01”,
3.“ 23:59”,“ 15:00”
4.“ 00:00”,“ 0:00”
时间格式不匹配:
1.“ 24:00” –⼩时超出范围[0-23]
2.“ 12:60” –分钟超出范围[00-59]
3.“ 0:0” –分钟的⽆效格式,⾄少2位数字
4.“ 13:1” –分钟的⽆效格式,⾄少2位数字
5.“ 101:00” –⼩时超出范围[0-23]
单元测试– Time24HoursValidatorTest package ;
stng.Asrt;
stng.annotations.*;
/**
* Time 24 hours format validator Testing
* @author mkyong
*
*/
public class Time24HoursValidatorTest {
private Time24HoursValidator time24HoursValidator;
@BeforeClass
public void initData(){
time24HoursValidator = new Time24HoursValidator();
}
@DataProvider
public Object[][] ValidTime24HoursProvider() {
return new Object[][]{
new Object[] {"01:00"}, new Object[] {"02:00"},
new Object[] {"13:00"}, new Object[] {"1:00"},
new Object[] {"2:00"},new Object[] {"13:01"},
new Object[] {"23:59"}, new Object[] {"15:00"},
new Object[] {"00:00"}, new Object[] {"0:00"}
};
}
@DataProvider
public Object[][] InvalidTime24HoursProvider() {
return new Object[][]{
new Object[] {"24:00"},new Object[] {"12:60"},
new Object[] {"0:0"},new Object[] {"13:1"},
new Object[] {"101:00"}
};
}
@Test(dataProvider = "ValidTime24HoursProvider")
public void ValidTime24HoursTest(String time) {
boolean valid = time24HoursValidator.validate(time);
System.out.println("Time24Hours is valid : " + time + " , " + valid); Asrt.asrtEquals(true, valid);
}
@Test(dataProvider = "InvalidTime24HoursProvider",
dependsOnMethods="ValidTime24HoursTest")
public void InValidTime24HoursTest(String time) {
boolean valid = time24HoursValidator.validate(time);
System.out.println("Time24Hours is valid : " + time + " , " + valid); Asrt.asrtEquals(fal, valid);
}
}
单元测试–结果
Time24Hours is valid : 01:00 , true
Time24Hours is valid : 02:00 , true
Time24Hours is valid : 13:00 , true
Time24Hours is valid : 1:00 , true
Time24Hours is valid : 2:00 , true
Time24Hours is valid : 13:01 , true
Time24Hours is valid : 23:59 , true
Time24Hours is valid : 15:00 , true
Time24Hours is valid : 00:00 , true
Time24Hours is valid : 0:00 , true
Time24Hours is valid : 24:00 , fal
Time24Hours is valid : 12:60 , fal
Time24Hours is valid : 0:0 , fal
Time24Hours is valid : 13:1 , fal
Time24Hours is valid : 101:00 , fal
PASSED: ValidTime24HoursTest("01:00")
PASSED: ValidTime24HoursTest("02:00")
PASSED: ValidTime24HoursTest("13:00")
PASSED: ValidTime24HoursTest("1:00")
PASSED: ValidTime24HoursTest("2:00")
PASSED: ValidTime24HoursTest("13:01")
PASSED: ValidTime24HoursTest("23:59")
PASSED: ValidTime24HoursTest("15:00")
PASSED: ValidTime24HoursTest("00:00")
PASSED: ValidTime24HoursTest("0:00")
PASSED: InValidTime24HoursTest("24:00")
PASSED: InValidTime24HoursTest("12:60")
PASSED: InValidTime24HoursTest("0:0")
PASSED: InValidTime24HoursTest("13:1")
PASSED: InValidTime24HoursTest("101:00")
===============================================
Time24HoursValidatorTest
Tests run: 15, Failures: 0, Skips: 0
===============================================
===============================================
mkyong
Total tests run: 15, Failures: 0, Skips: 0
===============================================
参考
想更多地了解正则表达式? 强烈推荐这本经典的书–“掌握正则表达式”