判断时间的正则表达式

噻1314

噻1314

2016-02-19 09:14

今天图老师小编要向大家分享个判断时间的正则表达式教程,过程简单易学,相信聪明的你一定能轻松get!
普通方法为,分离出小时、分钟、秒分别进行判断:
public static boolean timeCheck(String time, String owner) {
//检查时间字符串time是否满足格式“HH:mm:ss”或“HH:mm”,若不满足显示相应消息,并返回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) {
//检查时间字符串time是否满足格式“HH:mm:ss”,若不满足显示相应消息,并返回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;
}
  真是很好很强大啊!!不过上述正则“((2[0-3])|([01]d)):[0-5]d(:[0-5]d)?”实现居然不能匹配“3:3:3”,郁闷!
  将正则改为“((2[0-3])|([0-1]?d)):[0-5]?d(:[0-5]?d)?”倒是可以匹配“3:3:3”这种了,但“3:65:34”显然是不对的,却又匹配出两个来,晕!
  将正则改为“((2[0-3])|([0-1]?d)):[0-5]?d(:[0-5]?d)”倒是可以匹配“3:3:3”,也能正确判断“3:65:34”,却又没法判断“3:34”这种格式了。唉~~~~
展开更多 50%)
分享

猜你喜欢

判断时间的正则表达式

Web开发
判断时间的正则表达式

正则表达式

Web开发
正则表达式

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

正则表达式口诀 正则表达式学习工具

Web开发
正则表达式口诀 正则表达式学习工具

正则表达式判断email和URL

Web开发
正则表达式判断email和URL

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

正则表达式的语法

Web开发
正则表达式的语法

正则表达式的应用

Web开发
正则表达式的应用

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

Word插入脚注后分节符自动变成分页符解决办法

Word插入脚注后分节符自动变成分页符解决办法

C# 16进制与字符串、字节数组之间的转换

C# 16进制与字符串、字节数组之间的转换
下拉加载更多内容 ↓