使用SimpleDateFormat类对时间的合法性进行校验,使用正则表达式校验时间字符串的合法性;
来源:互联网 发布:lcd1602与单片机连接图 编辑:程序博客网 时间:2024/04/26 20:05
使用SimpleDateFormat类对时间的合法性进行校验,使用正则表达式校验时间字符串的合法性;
上次我已经讲过如何使用正则表达式对时间的合法性进行校验,出门左转就是:点击查看如何使用正则表达式对时间的合法性进行校验;
后来同事又提供了另外一种解决方案,同样可以达到对时间的合法性进行校验的目的:使用SimpleDateFormat类,下面详细介绍:
先附上源码让大家跑一遍再说吧:
package cn.mike.javase.test;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.regex.Pattern;import org.junit.Test;public class DateLegalityCheck {/** * 这个方法用来演示对日期的合法性进行校验 */@Testpublic void test_1() {// 需要被校验的时间字符串,格式:yyyyMMddHHmmssString dateStr = "20160229231360";checkLegalityWithRegularExpression(dateStr);// 使用正则表达式进行校验;checkLegalityInClassSimpleDateFormat(dateStr);// 使用SimpleDateFormat类来进行校验;}// end method -test_1/** * 使用正则表达式校验时间字符串的合法性 * * @param dateStr * 将要被校验合法性的时间字符串,格式:yyyyMMddHHmmss */private void checkLegalityWithRegularExpression(String dateStr) {String dateRegex = "^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))0229))([0-1]?[0-9]|2[0-3])([0-5][0-9])([0-5][0-9])$";if (Pattern.matches(dateRegex, dateStr)) {System.out.println("In regular expression checking , " + dateStr + " is legal time string.");} else {System.out.println("In regular expression checking , " + dateStr + " is not legal time string.");}}// end method - checkLegalityWithRegularExpression/** * 使用SimpleDateFormat类对时间字符串的合法性进行校验 * * @param dateStr * 将要被校验合法性的时间字符串,格式:yyyyMMddHHmmss */private void checkLegalityInClassSimpleDateFormat(String dateStr) {SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");try {Date realDate = sdf.parse(dateStr);if (dateStr.equals(sdf.format(realDate))) {System.out.println("In SimpleDateFormat checking , " + dateStr + " is legal time string.");} else {System.out.println("In SimpleDateFormat checking , " + dateStr + " is not legal time string.");}// System.out.println(realDate.toString());} catch (ParseException e) {e.printStackTrace();}}// end method - checkLegalityInClassSimpleDateFormat/* * output : * In regular expression checking , 20160229231360 is not legal time string. * In SimpleDateFormat checking , 20160229231360 is not legal time string. * */}// end class - DateLegalityCheck
我们对时间进行校验的要求是:该时间在我们正常的生活的世界中必须真实存在,e.g.:20161215234059。
而诸如:20161332246162 ,该时间就不合法:因为不存在2016年13月32日24时61分62秒;
当然了,平年2月28,闰年2月29,都必须要考虑到;
上面的方法就可以很好地解决这个问题:上面的代码中使用正则表达式的我就不再赘述了,我讲一下使用SimpleDateFormat的工作原理:
1,首先,创建一个SimpleDateFormat对象,它的格式为:yyyyMMddHHmmss,
2,将将要被校验的时间字符串转化为一个日期(这里需要声明一下:这里转换的时候,会把一个不合法的时间也给转化为合法的另外一个时间,比如:会把:20160215121360转化为20160215121400也就是说,它自己会进位,把60秒进到一分钟等等...)而恰恰我们就是利用了它的这一点功能实现的对时间的合法性完成的校验;
3.比较转化前的时间字符串与转发后的时间字符串是否一致(equals),(I)如果一致,就说明,SimpleDateFormat没有对原始的使时间字符串进行诸如进位的操作,同时也说明原时间字符串为合法的时间,因为不需要进位;(II)如果不一致,说明SimpleDateFormat类对原始串进行了进位操作,说明原始串不合法,因为SimpleDateFormat类需要对其进行“翻译”(进位);因此原时间字符串就不是一个合法的时间;
下面我粘贴一下SimpleDateFormat对不合法日期的进位演示代码片:
package cn.mike.javase.test;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import org.junit.Test;public class SimpleDF_TimeCarryDemo {@Testpublic void timeAutoCarryDemo_InSimpleDF() throws ParseException {// 需要被校验的原始时间串:String dateStr = "20160229231360";System.out.println("Original time string : " + dateStr);SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");Date realDate = sdf.parse(dateStr);System.out.println("Parsed time string : " + sdf.format(realDate));/* * output : * Original time string : 20160229231360 * Parsed time string : 20160229231400 */}}
很明显可以看出:20160229231360已经进位到了:20160229231400;
- 使用SimpleDateFormat类对时间的合法性进行校验,使用正则表达式校验时间字符串的合法性;
- javascript日期和时间的合法性校验
- javascript日期和时间的合法性校验
- 校验IP合法性的正则
- 利用正则表达式检查时间的合法性
- 正则表达式判断时间的合法性
- js正则表达式检查时间的合法性
- ASP中用“正则表达式对象”来校验数据的合法性
- 校验email的合法性
- 身份证号码的合法性校验
- 校验xml的合法性
- (5) php校验时间的合法性 && work attitude 20151020 ):
- 润乾报表中对时间的自动计算和合法性校验
- 使用Git hook来校验push用户名的合法性
- java校验身份证的合法性
- 在ASP中用“正则表达式对象”来校验数据的合法性
- 在ASP中用“正则表达式对象”来校验数据的合法性
- 在ASP中用“正则表达式对象”来校验数据的合法性 (轉載)
- 九连环所有步骤
- 信息的加密方式学习
- swift string 函数
- 利用dom4j解析.xml之查询标签
- html03
- 使用SimpleDateFormat类对时间的合法性进行校验,使用正则表达式校验时间字符串的合法性;
- uboot-tiny4412启动流程(下)----如何将自己的裸板测试程序加入uboot中启动测试
- Android下的单位及相互之间的关系和换算
- html04
- JavaScript基础(一)--基础知识归纳
- 不定参数函数
- html05
- AndroidStudio使用SVN代码管理
- Scala退出循环体