java常用字符串校验工具方法
001 | package com.ctrip.ops.odb.syncer.view.utils; |
002 |
003 | import java.text.SimpleDateFormat; |
004 |
005 | import java.util.Date; |
006 |
007 | import java.util.List; |
008 |
009 | import java.util.Map; |
010 |
011 | import java.util.Set; |
012 |
013 | import java.util.regex.Matcher; |
014 |
015 | import java.util.regex.Pattern; |
016 |
017 | /** |
018 |
019 | * @info : 描述 检查工具类 |
020 |
021 | * @author : lhli |
022 |
023 | * @date : 2013-6-26 上午10:38:03 |
024 |
025 | */ |
026 |
027 | public class CheckUtil { |
028 |
029 | /** |
030 |
031 | * 判断字符串长度(字符串全为空格的为false) |
032 |
033 | * @param str |
034 |
035 | * @param min |
036 |
037 | * @param max |
038 |
039 | * @return |
040 |
041 | */ |
042 |
043 | public static Boolean checkStrLength(String str, Integer min, Integer max) { |
044 |
045 | return (!isEmpty(str) && str.length() >= min && str.length() <= max); |
046 |
047 | } |
048 |
049 | /** |
050 |
051 | * 判断字符串是不是数字 |
052 |
053 | * @param str |
054 |
055 | * @return |
056 |
057 | */ |
058 |
059 | public static Boolean checkNumber(String str) { |
060 |
061 | return !isEmpty(str) && str.matches( "^\\d+$" ); |
062 |
063 | } |
064 |
065 | /** |
066 |
067 | * 判断数组是否全部为空 |
068 |
069 | * @param o |
070 |
071 | * @return |
072 |
073 | */ |
074 |
075 | public static Boolean isAllEmpty(Object[] o) { |
076 |
077 | if (o == null ) |
078 |
079 | return true ; |
080 |
081 | for (Object tmp : o) { |
082 |
083 | if (!isEmpty(tmp)) { |
084 |
085 | return false ; |
086 |
087 | } |
088 |
089 | } |
090 |
091 | return true ; |
092 |
093 | } |
094 |
095 | |
096 |
097 | /** |
098 |
099 | * 判断字符串是不是null或无字符(trim后) |
100 |
101 | * @param o |
102 |
103 | * @return |
104 |
105 | */ |
106 |
107 | public static Boolean isEmpty(String o) { |
108 |
109 | return (o == null || o.trim().length() == 0 ); |
110 |
111 | } |
112 |
113 | /** |
114 |
115 | * 判断整形是否为null或0 |
116 |
117 | * |
118 |
119 | * @param value |
120 |
121 | * @return |
122 |
123 | */ |
124 |
125 | public static Boolean isEmpty(Integer value) { |
126 |
127 | return (value == null || value == 0 ); |
128 |
129 | } |
130 |
131 | /** |
132 |
133 | * 判断整形是否为null或0 |
134 |
135 | * |
136 |
137 | * @param value |
138 |
139 | * @return |
140 |
141 | */ |
142 |
143 | public static Boolean isEmpty(Long value) { |
144 |
145 | return (value == null || value == 0 ); |
146 |
147 | } |
148 |
149 | /** |
150 |
151 | * 判断List是否为空 |
152 |
153 | * |
154 |
155 | * @param list |
156 |
157 | * @return |
158 |
159 | */ |
160 |
161 | public static Boolean isEmpty(List<?> list) { |
162 |
163 | return (list == null || list.size() == 0 ); |
164 |
165 | } |
166 |
167 | /** |
168 |
169 | * 判断Map是否为空 |
170 |
171 | * @param map |
172 |
173 | * @return |
174 |
175 | */ |
176 |
177 | public static Boolean isEmpty(Map<?, ?> map) { |
178 |
179 | return (map == null || map.size() == 0 ); |
180 |
181 | } |
182 |
183 | /** |
184 |
185 | * 判断Set是否为空 |
186 |
187 | * @param set |
188 |
189 | * @return |
190 |
191 | */ |
192 |
193 | public static Boolean isEmpty(Set<?> set) { |
194 |
195 | return (set == null || set.size() == 0 ); |
196 |
197 | } |
198 |
199 | /** |
200 |
201 | * 判断Object是否为空 |
202 |
203 | * @param o |
204 |
205 | * @return |
206 |
207 | */ |
208 |
209 | public static Boolean isEmpty(Object o) { |
210 |
211 | // return o == null; |
212 |
213 | if (o == null ) { |
214 |
215 | return true ; |
216 |
217 | } |
218 |
219 | boolean isEmpty = false ; |
220 |
221 | if (o instanceof String) { |
222 |
223 | isEmpty = isEmpty((String) o); |
224 |
225 | } else if (o instanceof Integer) { |
226 |
227 | isEmpty = isEmpty((Integer) o); |
228 |
229 | } else if (o instanceof List<?>) { |
230 |
231 | isEmpty = isEmpty((Integer) o); |
232 |
233 | } else if (o instanceof Map<?, ?>) { |
234 |
235 | isEmpty = isEmpty((Integer) o); |
236 |
237 | } else if (o instanceof Set<?>) { |
238 |
239 | isEmpty = isEmpty((Integer) o); |
240 |
241 | } |
242 |
243 | return isEmpty; |
244 |
245 | } |
246 |
247 | /** |
248 |
249 | * 判断数组是否为空 |
250 |
251 | * @param o |
252 |
253 | * @return |
254 |
255 | */ |
256 |
257 | public static Boolean isEmpty(Object[] o) { |
258 |
259 | return (o == null || o.length == 0 ); |
260 |
261 | } |
262 |
263 | /** |
264 |
265 | * 根据指定的正则表达式验证字符串 |
266 |
267 | * @param regex 正则表达式 |
268 |
269 | * @param str 检验内容 |
270 |
271 | * @return |
272 |
273 | */ |
274 |
275 | public static Boolean checkRegex(String regex, String str) { |
276 |
277 | Pattern pattern = Pattern.compile(regex); |
278 |
279 | Matcher matcher = pattern.matcher(str); |
280 |
281 | return matcher.matches(); |
282 |
283 | } |
284 |
285 | /** |
286 |
287 | * 验证正整数 |
288 |
289 | * @param str 检验内容 |
290 |
291 | * @param min 最小长度 |
292 |
293 | * @param max 最大长度 |
294 |
295 | * @return |
296 |
297 | */ |
298 |
299 | public static Boolean checkPositive(String str, int min, int max) { |
300 |
301 | return checkRegex( "^\\d{" + min + "," + max + "}$" , str); |
302 |
303 | } |
304 |
305 | /** |
306 |
307 | * 验证正整数 |
308 |
309 | * @param str 检验内容 |
310 |
311 | * @param length 整数的长度 |
312 |
313 | * @return |
314 |
315 | */ |
316 |
317 | public static Boolean checkPositive(String str, int length) { |
318 |
319 | return checkRegex( "^\\d{" + length + "}$" , str); |
320 |
321 | } |
322 |
323 | /** |
324 |
325 | * 判断Object数组中的值是否为空,只要其中有一个为空就返回true<br/> |
326 |
327 | * Integer为0会判断为空 |
328 |
329 | * @param o |
330 |
331 | * @return |
332 |
333 | */ |
334 |
335 | public static boolean oneMoreEmpty(Object[] o) { |
336 |
337 | boolean b = false ; |
338 |
339 | for ( int i = 0 ; i < o.length; i++) |
340 |
341 | if (isEmpty(o[i])) { |
342 |
343 | b = true ; |
344 |
345 | break ; |
346 |
347 | } |
348 |
349 | return b; |
350 |
351 | } |
352 |
353 | |
354 |
355 | /** |
356 |
357 | * 判断时间是否是今天 |
358 |
359 | * 是今天时间,返回true |
360 |
361 | * 非今天时间,返回false |
362 |
363 | * @param date |
364 |
365 | * @return |
366 |
367 | */ |
368 |
369 | public static boolean timeIsTodayTime(Date date){ |
370 |
371 | if (date== null ){ |
372 |
373 | return false ; |
374 |
375 | } |
376 |
377 | SimpleDateFormat sdf= new SimpleDateFormat( "yyyy-MM-dd" ); |
378 |
379 | String paramDate=sdf.format(date); |
380 |
381 | String nowDate=sdf.format( new Date()); |
382 |
383 | return paramDate.equals(nowDate); |
384 |
385 | } |
386 |
387 | // public static void main(String[] args) { |
388 |
389 | // System.out.println(checkPositive("123121", 5)); |
390 |
391 | // System.out.println(checkPositive("121", 5,8)); |
392 |
393 | // } |
394 |
395 | /** |
396 |
397 | * 根据指定的正则表达式校验字符串 |
398 |
399 | * |
400 |
401 | * @param reg |
402 |
403 | * 正则表达式 |
404 |
405 | * @param string |
406 |
407 | * 拼配的字符串 |
408 |
409 | * @return |
410 |
411 | */ |
412 |
413 | public static boolean startCheck(String reg, String string) { |
414 |
415 | if (isEmpty(string)) { |
416 |
417 | return false ; |
418 |
419 | } |
420 |
421 | boolean tem = false ; |
422 |
423 | Pattern pattern = Pattern.compile(reg); |
424 |
425 | Matcher matcher = pattern.matcher(string); |
426 |
427 | tem = matcher.matches(); |
428 |
429 | return tem; |
430 |
431 | } |
432 |
433 | |
434 |
435 | public static final String REGEX_MOBILE = "^0?1(?:3[0-9]|4[457]|5[0-35-9]|8[0-35-9])\\d{8}$" ; |
436 |
437 | /** |
438 |
439 | * 手机号码验证,11位 13 号段0-9 14 号段 5,7 15 号段除4以外 18 号段 6, 7, 8, 9 |
440 |
441 | * */ |
442 |
443 | public static boolean checkCellPhone(String cellPhoneNr) { |
444 |
445 | // String reg = "^(13[0-9]|14[57]|15[^4]|18[6-9])\\d{8}$"; |
446 |
447 | return startCheck(REGEX_MOBILE, cellPhoneNr); |
448 |
449 | } |
450 |
451 | |
452 |
453 | public static final String REGEX_PHONE = "^(0[0-9]{2,3})?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?$|(^400[0-9]{7}$)" ; |
454 |
455 | public static boolean checkTel(String phone) { |
456 |
457 | return startCheck(REGEX_PHONE, phone); |
458 |
459 | } |
460 |
461 | } |