标签:style blog io color ar os java sp for
1 /** 2 * 3 */ 4 package com.ssh.util; 5 6 import java.util.regex.Matcher; 7 import java.util.regex.Pattern; 8 9 /** 10 * @author leorain 11 * 12 */ 13 public class ValidateUtil { 14 /** 15 * 手机号验证 16 * 17 * @param str 18 * @return 验证通过返回true 19 */ 20 public static boolean isMobile(String str) { 21 Pattern p = null; 22 Matcher m = null; 23 boolean b = false; 24 p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 验证手机号 25 m = p.matcher(str); 26 b = m.matches(); 27 return b; 28 } 29 /** 30 * 电话号码验证 31 * 32 * @param str 33 * @return 验证通过返回true 34 */ 35 public static boolean isPhone(String str) { 36 Pattern p1 = null,p2 = null; 37 Matcher m = null; 38 boolean b = false; 39 p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$"); // 验证带区号的 40 p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$"); // 验证没有区号的 41 if(str.length() >9) 42 { m = p1.matcher(str); 43 b = m.matches(); 44 }else{ 45 m = p2.matcher(str); 46 b = m.matches(); 47 } 48 return b; 49 } 50 /** 51 * 日期格式验证 52 * @param str 53 * @return 验证通过返回true 54 * */ 55 public static boolean formateDate(String str){ 56 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]))))))"; 57 Pattern p = Pattern.compile(eL); 58 Matcher m = p.matcher(str); 59 boolean flag = m.matches(); 60 return flag; 61 } 62 63 /** 64 * @param t 65 * @return 66 */ 67 public static <T extends java.util.Date> java.util.Date toUtil(T t){ 68 java.util.Date date = t; 69 return date; 70 } 71 72 73 enum Type { 74 DATE, TIME, TIMESTAMP 75 } 76 @SuppressWarnings("unchecked") 77 public static <T extends java.util.Date> T toSql(java.util.Date utilDate, 78 Type type) { 79 T t = null; 80 long millionSeconds = utilDate.getTime(); 81 82 switch (type) { 83 case DATE: 84 t = (T) new java.sql.Date(millionSeconds); 85 break; 86 case TIME: 87 t = (T) new java.sql.Time(millionSeconds); 88 break; 89 case TIMESTAMP: 90 t = (T) new java.sql.Timestamp(millionSeconds); 91 break; 92 default: 93 break; 94 } 95 return t; 96 } 97 /** 98 * 99 * @param a 100 * 被匹配的长字符串 101 * @param b 102 * 匹配的短字符串 103 * @return 匹配次数 104 */ 105 public int hit(String a, String b) { 106 if (a.length() < b.length()) { 107 return 0; 108 } 109 char[] a_t = a.toCharArray(); 110 char[] b_t = b.toCharArray(); 111 int count = 0, temp = 0, j = 0; 112 113 for (int i = 0; i < a_t.length; i++) { 114 // 保证一个连续的字符串 b 跟 a中某段相匹配 115 if (a_t[i] == b_t[j] && j < b_t.length) { 116 temp++; 117 j++; 118 // 此时连续的字符串 b 跟 已跟 a 中某段相匹配 119 if (temp == b_t.length) { 120 count++; 121 temp = 0; 122 j = 0; 123 } 124 } 125 // 只要有一个字符不匹配,temp计数从来 126 else { 127 temp = 0; 128 j = 0; 129 } 130 } 131 132 return count; 133 } 134 }
标签:style blog io color ar os java sp for
原文地址:http://www.cnblogs.com/leorain/p/4093603.html