DDR爱好者之家 Design By 杰米
普通方法为,分离出小时、分钟、秒分别进行判断:
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”这种格式了。唉~~~~
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”这种格式了。唉~~~~
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2024年11月25日
2024年11月25日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]