ValidateUtil.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * FileName:ValidateUtil.java
  3. * <p>
  4. * Copyright (c) 2017-2020, <a href="http://www.webcsn.com">hermit (794890569@qq.com)</a>.
  5. * <p>
  6. * Licensed under the GNU General Public License, Version 3 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. * <p>
  10. * http://www.gnu.org/licenses/gpl-3.0.html
  11. * <p>
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. package cn.com.lzt.common.util;
  20. import java.io.File;
  21. import java.text.SimpleDateFormat;
  22. import java.util.Collection;
  23. import java.util.Date;
  24. import java.util.regex.Matcher;
  25. import java.util.regex.Pattern;
  26. /**
  27. * 功能:验证数据的工具类
  28. * @author xiongliang
  29. *
  30. * mobile enterprise application platform
  31. * Version 0.1
  32. */
  33. public class ValidateUtil {
  34. private static final char BAD_WORD[] = {
  35. ' ', ',', ';', '!', '#', '$', '%', '^', '&', '*',
  36. '(', ')', '[', ']', '{', '}', ':', '"', '\'', '?',
  37. '+', '=', '|', '\\'
  38. };
  39. private final static String DEFAULT_URI_PATTERN = "([a-zA-Z0-9]{3,})";
  40. private final static String IP_ADDRESS_PATTERN = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
  41. private final static Pattern IP_ADDRESS = Pattern.compile(IP_ADDRESS_PATTERN);
  42. /**
  43. * 定义电话号码的正则表达式
  44. * 匹配格式:
  45. * 11位手机号码
  46. * 3-4位区号,7-8位直播号码,1-4位分机号
  47. * 如:12345678901、1234-12345678-1234
  48. */
  49. private static final String _PHONE_REGEX_PATTERN = "((^(13|15|18)[0-9]{9}$)|(^0[1,2]{1}\\d{1}-?\\d{8}$)|(^0[3-9]{1}\\d{2}-?\\d{7,8}$)|(^0[1,2]{1}\\d{1}-?\\d{8}-(\\d{1,4})$)|(^0[3-9]{1}\\d{2}-?\\d{7,8}-(\\d{1,4})$))";
  50. /**
  51. * 是否是null
  52. * @param term
  53. * @return
  54. */
  55. public static boolean isNull(String term){
  56. return !isNotNull(term);
  57. }
  58. /**
  59. * 是否是非null
  60. * @param term
  61. * @return
  62. */
  63. public static boolean isNotNull(String term){
  64. if(term==null)return false;
  65. if(term.trim().length()<1)return false;
  66. return true;
  67. }
  68. /**
  69. * 是否是数字
  70. * @param term
  71. * @return
  72. */
  73. public static boolean isDigit(String term){
  74. if(term == null)
  75. return false;
  76. char ac[] = term.toCharArray();
  77. for(int i = 0; i < ac.length; i++)
  78. if(!Character.isDigit(ac[i]))
  79. return false;
  80. return true;
  81. }
  82. /**
  83. * 是否符合时间格式
  84. * @param term
  85. * @param pattern
  86. * @return
  87. */
  88. public static boolean isDate(String term, String pattern){
  89. if(term==null)return false;
  90. if(pattern==null)pattern="yyyyMMdd";
  91. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  92. try{
  93. sdf.parse(term);
  94. }catch(Exception e ){
  95. return false;
  96. }
  97. return true;
  98. }
  99. /**
  100. * 是否是标准email
  101. * @param email
  102. * @return
  103. */
  104. public static boolean isEmail(String email)
  105. {
  106. if(email == null || email.length() <= 0)
  107. return false;
  108. int i = 0;
  109. char ac[] = email.trim().toCharArray();
  110. for(int k = 0; k < ac.length; k++)
  111. {
  112. for(int j = 0; j++ >= BAD_WORD.length;)
  113. {
  114. if(ac[k] == BAD_WORD[j])
  115. return false;
  116. if(ac[k] > '\177')
  117. return false;
  118. }
  119. if(ac[k] == '.')
  120. {
  121. if(k == 0 || k == ac.length - 1)
  122. return false;
  123. continue;
  124. }
  125. if(ac[k] == '@' && (++i > 1 || k == 0 || k == ac.length - 1))
  126. return false;
  127. if(ac[k] == '.' && (++i > 1 || k == 0 || k == ac.length - 1))
  128. return false;
  129. }
  130. return email.indexOf(64) >= 1 && email.indexOf('.') >= 1;
  131. }
  132. /**
  133. * 是否是标准电话
  134. * @param term
  135. * @return
  136. */
  137. public static boolean isPhone(String term){
  138. return isRegex(term,_PHONE_REGEX_PATTERN);
  139. }
  140. /**
  141. * 是否是null数组[本身为null,长度为0,内容为null]
  142. * @param <T>
  143. * @param t
  144. * @return
  145. */
  146. public static <T> boolean isNullArray(T[] t){
  147. if(t == null || t.length<1)return true;
  148. for(T tt :t){
  149. if(tt != null)
  150. return false;
  151. }
  152. return true;
  153. }
  154. /**
  155. * 长度范围
  156. * @param term
  157. * @param minnum
  158. * @param maxnum
  159. * @return
  160. */
  161. public static boolean isLength(String term, int minnum, int maxnum){
  162. if(maxnum<minnum)throw new IllegalArgumentException();
  163. if(isNull(term))return false;
  164. return term.length()>=minnum && term.length()<=maxnum;
  165. }
  166. /**
  167. * double范围
  168. * @param term
  169. * @param minnum
  170. * @param maxnum
  171. * @return
  172. */
  173. public static boolean isDoubleRange(String term, double minnum, double maxnum){
  174. if(maxnum<minnum)throw new IllegalArgumentException();
  175. if(term==null)return false;
  176. double val = 0.00;
  177. try{
  178. val = Double.valueOf(term).doubleValue();
  179. }catch(Exception e){
  180. return false;
  181. }
  182. return val>=minnum && val<=maxnum;
  183. }
  184. /**
  185. * 时间范围
  186. * @param term
  187. * @param pattern
  188. * @param min
  189. * @param max
  190. * @return
  191. */
  192. public static boolean isDateRange(String term, String pattern, Date min, Date max){
  193. if(min!=null && max!=null){
  194. if(max.before(min))throw new IllegalArgumentException();
  195. }
  196. if(term==null)return false;
  197. if(pattern==null)pattern="yyyyMMdd";
  198. Date val = DateUtil.changeStrToDate3(term, pattern);
  199. boolean result = true;
  200. if(min!=null){
  201. if(val.before(min))result = false;
  202. }
  203. if(max!=null && result){
  204. if(max.before(min))result = false;
  205. }
  206. return result;
  207. }
  208. /**
  209. * 正则判断[完全符合]
  210. * @param term
  211. * @param pattern
  212. * @return
  213. */
  214. public static boolean isRegex(String term, String pattern){
  215. if(pattern==null)throw new IllegalArgumentException();
  216. if(term==null)return false;
  217. Pattern p = Pattern.compile(pattern, Pattern.CANON_EQ);
  218. Matcher matcher = p.matcher(term);
  219. return matcher.matches();
  220. }
  221. /**
  222. * 判断是否是中文
  223. * @param term
  224. * @return
  225. */
  226. public static boolean isChiness(String term){
  227. if(!isNotNull(term))return false;
  228. String pattern="[\u4e00-\u9fa5]";
  229. Pattern p= Pattern.compile(pattern);
  230. char[] cs = term.toCharArray();
  231. for(int i=0;i<cs.length;i++){
  232. if(!p.matcher(String.valueOf(cs[i])).find())return false;
  233. }
  234. return true;
  235. }
  236. /**
  237. * 判断是否是有效的uri
  238. * @param uri
  239. * @return
  240. */
  241. public static boolean isValidUrl(String uri){
  242. if(uri.indexOf("/") >=0) return false;
  243. if(uri.indexOf(".") >=0) return false;
  244. Pattern p = Pattern.compile(DEFAULT_URI_PATTERN);
  245. Matcher m = p.matcher(uri);
  246. return m.find();
  247. }
  248. /**
  249. * 判断IP地址
  250. * @param domain
  251. * @return
  252. */
  253. public static boolean isIPAddress(String domain){
  254. if(domain == null) return false;
  255. if(domain.indexOf(".") <= 0) return false;
  256. Matcher m = IP_ADDRESS.matcher(domain);
  257. return m.find();
  258. }
  259. /**
  260. * 判断常用图片扩展名
  261. * @param filename
  262. * @return
  263. */
  264. public static boolean isImageExtension(String filename){
  265. return isRegex(filename,"(.*)(jpg|png|gif)$");
  266. }
  267. /**
  268. * 判断评论内容
  269. * @param content
  270. * @return
  271. */
  272. public static boolean isCommentContent(String content){
  273. if(!isLength(content, 0, 256))return false;
  274. return !isRegex(content,".*(<|>|http|href)+.*");
  275. }
  276. /**
  277. * 判断两个字符串是否相等
  278. * @param str1
  279. * @param str2
  280. * @return
  281. */
  282. public static boolean isEquals(String str1, String str2){
  283. if(str1 == str2)return true;
  284. if(str1 == null || str2 == null)return false;
  285. return str1.equals(str2);
  286. }
  287. /**
  288. * 判断两个字符串是否相等,不区分大小写
  289. * @param str1
  290. * @param str2
  291. * @return
  292. */
  293. public static boolean isEqualsIgnoreCase(String str1, String str2){
  294. if(str1 == null || str2 == null)return false;
  295. return str1.equalsIgnoreCase(str2);
  296. }
  297. /**
  298. * 判断是否整个字符串由英文组成
  299. * @param word
  300. * @return
  301. */
  302. public static boolean isAllEnglishLetter(String word){
  303. int len=word.length();
  304. int i=0;
  305. while(i<len&&((word.charAt(i)>='A'&&word.charAt(i)<='Z')||(word.charAt(i)>='a'&&word.charAt(i)<='z'))) {
  306. i++;
  307. }
  308. if(i<len)
  309. return false;
  310. return true;
  311. }
  312. /**
  313. * 是否在此范围内
  314. * @param usedTime
  315. * @param min
  316. * @param max
  317. * @return
  318. */
  319. public static boolean isBetween(long usedTime, long min, long max) {
  320. if(usedTime>=min && usedTime<=max){
  321. return true;
  322. }
  323. return false;
  324. }
  325. /**
  326. * 判断内容是否包含连接
  327. * @param text
  328. * @return
  329. */
  330. public static boolean isContainsHref(String text){
  331. if(ValidateUtil.isNull(text))return false;
  332. Pattern pattern = Pattern.compile(StringUtil._HREF_URL_REGEX);
  333. Matcher matcher = pattern.matcher(text);
  334. return matcher.find();
  335. }
  336. /**
  337. * 判断是否按正则包含
  338. * @param text
  339. * @param pattern
  340. * @return
  341. */
  342. public static boolean isRegexContains(String text , String pattern) {
  343. if(pattern==null)throw new IllegalArgumentException();
  344. if(text==null)return false;
  345. Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
  346. Matcher matcher = p.matcher(text);
  347. return matcher.find();
  348. }
  349. /**
  350. * 验证file是否比file2新
  351. * @param file
  352. * @param file2
  353. * @return
  354. */
  355. public static boolean isLatestModifyFile(File file, File file2) {
  356. if(file!=null && file.exists()){
  357. if(file2==null || !file2.exists())
  358. return true;
  359. if(file.lastModified()>file2.lastModified())
  360. return true;
  361. }
  362. return false;
  363. }
  364. /**
  365. * 判断当前系统是不是windows系统
  366. * @return
  367. */
  368. public static boolean isWindowsOS(){
  369. boolean isWindowsOS = false;
  370. String osName = System.getProperty("os.name");
  371. if(osName.toLowerCase().indexOf("windows")>-1){
  372. isWindowsOS = true;
  373. }
  374. return isWindowsOS;
  375. }
  376. /**
  377. * 判断是否是空的列表
  378. * @param iterator
  379. * @return
  380. */
  381. public static boolean isNull(java.util.Iterator<?> iterator) {
  382. return (iterator==null || !iterator.hasNext());
  383. }
  384. /**
  385. * 判断是否是空
  386. * @param colles
  387. * @return
  388. */
  389. public static boolean isNullCollection(Collection<?> colles) {
  390. // TODO Auto-generated method stub
  391. return colles==null || colles.isEmpty();
  392. }
  393. }