1

在请假申请表中,有两个时间字段“从”和“到”。当我说,在 From 时间字段中的 12:30 PM 和在 To 时间字段中的 1:30 PM 时,验证结果显示“To”时间应该大于“From”时间。是否有任何简单的验证(可能是正则表达式)可用于验证这一点,以便它应该以 12 小时格式接受这个时间。

任何建议都将是可观的。

谢谢。

4

2 回答 2

1

确保您小心处理过境日/周/月/年/...边界。

您可能有兴趣查看http://infiniteundo.com/post/25326999628/falsehoods-programmers-believe-about-time

于 2012-06-28T05:07:04.623 回答
1

您可以使用SimpleDateFormatter作为此示例:

DateFormat formatter = new SimpleDateFormat("hh:mm a");
Date from = (Date)formatter.parse(fromText);
Date to= (Date)formatter.parse(toText);

if (from.after(to)) {
    // Show message as you want.
    return;
}

如果要验证,只需在 parse() 方法上添加 try/catch,并显示消息。

try {
    Date from = (Date)formatter.parse(fromText);
}
catch(Exception e) {
    // Show message;
    return;
}
于 2012-06-28T05:08:54.777 回答