-
java文件中校验日期和时间格式,急求解答0
现在需要在java文件中校验日期是否符合yyyy-mm-dd格式并且为有效日期,也就是说不能出现0000-00-00这种的错误形式,请问有没有什么好办法?另外,还要校验时间格式,时间格式为 xx:xx:xx,时间不允许出现24:00:00这种情况。请问有没有对这方面比较牛的,麻烦给解答一下了2012年8月30日 10:37
6个答案 按时间排序 按投票排序
-
采纳的答案
static int[] DAYS = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /** * @param date yyyy-MM-dd HH:mm:ss * @return */ public static boolean isValidDate(String date) { try { int year = Integer.parseInt(date.substring(0, 4)); if (year <= 0) return false; int month = Integer.parseInt(date.substring(5, 7)); if (month <= 0 || month > 12) return false; int day = Integer.parseInt(date.substring(8, 10)); if (day <= 0 || day > DAYS[month]) return false; if (month == 2 && day == 29 && !isGregorianLeapYear(year)) { return false; } int hour = Integer.parseInt(date.substring(11, 13)); if (hour < 0 || hour > 23) return false; int minute = Integer.parseInt(date.substring(14, 16)); if (minute < 0 || minute > 59) return false; int second = Integer.parseInt(date.substring(17, 19)); if (second < 0 || second > 59) return false; } catch (Exception e) { e.printStackTrace(); return false; } return true; } public static final boolean isGregorianLeapYear(int year) { return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); } public static void main(String[] args) { System.out.println(isValidDate("2100-02-29 23:00:01")); }
2012年8月30日 16:22
-
用一个指定的format去parse,有异常就是日期格式不对:
public boolean isValidDate(String s) { try { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.parse(s); return true; } catch (Exception e) { // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对 return false; } }
2012年8月30日 11:46
-
其实你在 入库的时候,可以先 弄一个时间的 js控件,这样就能保障数据的正确性了
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; public class DateTimeCheck { /** * 验证日期时间格式 * @param args */ public static void main(String[] args) { String checkValue = "2008-09-11 14:17:11"; DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); Date d = null; if(checkValue != null && !checkValue.equals("")) { if(checkValue.split("/").length > 1) { dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); } if (checkValue.split("-").length > 1) { dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } }else { return; } try { d = dateFormat.parse(checkValue); System.out.println(d); } catch(Exception e) { System.out.println("格式错误"); return; } String eL= "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-9]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$"; Pattern p = Pattern.compile(eL); Matcher m = p.matcher(checkValue); boolean b = m.matches(); if(b) { System.out.println("格式正确"); } else { System.out.println("格式错误"); } } }
2012年8月30日 11:29
-
正则表达式 + 组
如(\\d{4})-(\\d{2})(\\d{2}),提取分组中的数据[即()中] 再判断每个分组数据。
或者先定义不合法的格式进行验证
(00|24)00-00-002012年8月30日 11:20
相关推荐
"yyyyMM","yyyyMMdd","yyyyMMdd HH:mm:ss", "yyyy-MM","yyyy-MM-dd","yyyy-MM-dd HH:mm:ss" "yyyy.MM","yyyy.MM.dd","yyyy.MM.dd HH:mm:ss" "yyyy/MM","yyyy/MM/dd","yyyy/MM/dd HH:mm:ss" "yyyy_MM","yyyy_MM_dd",...
java验证文件真实格式和编码格式工具类,验证文件的文件格式和文件的编码格式给出正确的文件类型以及编码格式,从此解决文件解析乱码问题的烦恼
* @param 日期验证,验证的格式有: * "yyyyMM","yyyyMMdd","yyyyMMdd HH:mm:ss", * "yyyy-MM","yyyy-MM-dd","yyyy-MM-dd HH:mm:ss" * "yyyy.MM","yyyy.MM.dd","yyyy.MM.dd HH:mm:ss" * "yyyy/MM","yyyy/MM/dd...
通过MD5,计算文件的校验和.程序使用JAVA语言编写,用户可方便地改为C#或其他语言。
Java实现MD5大文件校验码详细解析: 包括具体方法解释、注释 通俗易懂、易于理解
由于需要和蓝牙通讯,协议需要用到校验和,找了很久才找到,给大家共享。java校验和算法绝对可以用。
JAVA中进行数据完整性验证 最近在看JAVA安全方面的东东。简单地说,安全包括访问控制、数据安全两部分。安全访问控制是根据系统需求进行设计的,对资源进行访问控制的一种措施。而数据安全包括数据传输过程中的安全...
java 怎么去校验时间,请参考这实现.谢谢
java socket在传输文件时,分包、组包和校验。 校验包数据,现成的代码可以拷贝直接运行。
Java计算校验和源码,已测试通过
通过Java读取Excel文件中的数据时,需要自动判断当前Cell中的数据格式是否日期格式,如果是就要做特殊处理,以便读出数据的格式与预想的相符。
针对身份证号码的校验,以及能自动将身份证从15位升级为18位
本工具为 计算文件校验和工具。请勿作为商业用途,搭配本人博客理解更佳:https://blog.csdn.net/JavaBuilt/article/details/79583921
JAVA_MAC地址校验和转换,MAC地址递增,MAC地址加密
主要介绍了java实现上传文件类型检测过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
使用Java代码校验IP格式的正确性,可以穿插在功能代码中使用。
java正则校验,手机号,邮箱,日期格式,时间格式,数字金额两位小数 3.58是否为金额:true 3.582是否为金额:false 1284789@qq.com是否为email:true 1284789qq.com是否为email:false 1823753112是否为手机号:...
java通过文件头内容判断文件类型
java实现CRC16校验功能~ 测试下载,直接运行即可!用的好的给个好评~
Java校验身份证号码工具类 ---运行几年了,很靠谱。包含15位身份证校验Java校验身份证号码工具类 ---运行几年了,很靠谱。包含15位身份证校验