java邮箱校验,手机号校验,格式化日期等等

1import java.text.SimpleDateFormat; 2import java.util.Calendar; 3import java.util.regex.Matcher; 4import java.util.regex.Pattern; 5 6import 这个是传入数据的封装jar包,可以不用看,具体看自己需求context.Recordset; 7 8public class ApplyUtil { 9 public final static String Number_Chinese = "1"; //2012年2月16日 10 public final static String Number_Point = "2";//2010.2.16 11 /** 12 * 判断数据是否为空 13 * @param str 字符串 14 * @return 15 */ 16 public static boolean isEmpty(String str){ 17 if(null == str || str.trim().equals("")){ 18 return true; 19 } 20 return false; 21 } 22 23 /** 24 * 判断记录集是否为空 25 * @param rs 26 * @return 27 */ 28 public static boolean isRecordsetNull(Recordset rs){ 29 if(null == rs || rs.size() == 0){ 30 return true; 31 } 32 return false; 33 } 34 35 /** 36 * 为当前日期增加month月 37 * @time 2012-6-6 上午10:57:32 38 * @param month 39 * @return 40 */ 41 public static String addMonth(int month){ 42 Calendar cal = Calendar.getInstance(); 43 SimpleDateFormat sFmt = new SimpleDateFormat("yyyy年MM月dd日"); 44 if (month != 0) { 45 cal.add(Calendar.MONTH, month); 46 } 47 return sFmt.format(cal.getTime()); 48 } 49 50 51 /** 52 * 根据要求将时间转化成字符串 53 * @param date 8位字符 54 * @param style 样式 55 * @return 56 */ 57 public static String parseDate2String(String date,String style){ 58 String formatDate = ""; 59 int len = 0; 60 if(!ApplyUtil.isEmpty(date)){ 61 len = date.trim().length(); 62 if(len == 8){ 63 if(Number_Chinese.equals(style)){ 64 formatDate = getNumberChineseDate(date); 65 } 66 if(Number_Point.equals(style)){ 67 formatDate = getNumberPointDate(date); 68 } 69 }else{ 70 formatDate = date; 71 } 72 } 73 return formatDate; 74 } 75 76 /** 77 * 格式化日期为:2012年2月16日 78 * @param date 79 * @return 80 */ 81 private static String getNumberChineseDate(String date){ 82 String year = date.substring(0, 4); 83 String month = leaveZero(date.substring(4, 6)); 84 String day = leaveZero(date.substring(6,8)); 85 return year + "年" + month + "月" + day + "日"; 86 } 87 88 /** 89 * 格式化日期为:2012年2月16日 90 * @param date 91 * @return 92 */ 93 private static String getNumberPointDate(String date){ 94 String year = date.substring(0, 4); 95 String month = leaveZero(date.substring(4, 6)); 96 String day = leaveZero(date.substring(6,8)); 97 return year + "." + month + "." + day; 98 } 99 100 /** 101 * 去掉前面含有的0 102 * @param str 103 * @return 104 */ 105 private static String leaveZero(String str){ 106 if(str.length() == 2 && str.charAt(0) == '0'){ 107 return str.substring(1,2); 108 } 109 return str; 110 } 111 112 /** 113 * 获得文件的类型 114 * @author ly 115 * @date 2012-4-9 116 * @param file 文件的全路径 117 * @return 118 */ 119 public static String getSuffix(String file) { 120 if(isEmpty(file) || file.indexOf(".") == -1 || file.indexOf(".") == file.length() - 1) 121 return ""; 122 System.out.println(file.length()); 123 System.out.println(file.lastIndexOf(".")); 124 String suffix=file.substring(file.lastIndexOf("."),file.length()); 125 return suffix.toLowerCase(); 126 } 127 128 129 /** 130 * 邮箱校验 131 * @param email 132 * @return true:邮箱格式正确; false:邮箱格式错误 133 */ 134 public static boolean isEmail(String email){ 135 //String str = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";//带横杠的出错 136 //String str = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";//带下划线出错 137// Pattern p = Pattern.compile(str); 138// Matcher m = p.matcher(email.trim()); 139// return m.matches(); 140 //Pattern pattern = Pattern.compile("[0-9a-zA-Z]*.[0-9a-zA-Z]*@[a-zA-Z]*.[a-zA-Z]*", Pattern.LITERAL); 141 if(email == null){ 142 return false; 143 } 144 145 //验证开始 146 147 //不能有连续的. 148 if(email.indexOf("..") != -1){ 149 return false; 150 } 151 152 //必须带有@ 153 int atCharacter = email.indexOf("@"); 154 if (atCharacter == -1) { 155 return false; 156 } 157 158 //最后一个.必须在@之后,且不能连续出现 159 if(atCharacter > email.lastIndexOf('.') || atCharacter+1 == email.lastIndexOf('.')){ 160 return false; 161 } 162 163 //不能以.,@结束和开始 164 if (email.endsWith(".") || email.endsWith("@") || email.startsWith(".") || email.startsWith("@")) { 165 return false; 166 } 167 return true; 168 169 } 170 171 /** 172 * 手机号校验 173 * @param mobiles 174 * @return true:手机号格式正确; false:手机号格式不正确 175 */ 176 public static boolean isMobileNO(String mobiles){ 177 String str = "^1[3|4|5|8][0-9][0-9]{8}$"; 178 //String str = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"; 179 Pattern p = Pattern.compile(str); 180 Matcher m = p.matcher(mobiles.trim()); 181 return m.matches(); 182 } 183 184}
点赞
收藏

评论区

加载中...

相关推荐

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

一篇文章带你了解JavaScript日期

日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用

皕杰报表之数据校验与处理

填报校验分为四种:长度验证、内容校验、计算校验、JavaScript校验。长度校验非空验证就是验证该单元格的值不能为空。内容校验适宜于验证输入内容的格式,譬如:邮箱验证、身份证验证、手机号码验证、邮政编码验证等。计算效验计算效验就是可以进行一些简单的计算校验。JavaScript校验JavaScript校验就是可以与使用一些javascript语言进行校验。

Java中的参数验证(非Spring版)

1\.Java中的参数验证(非Spring版)1.1.前言为什么我总遇到这种非正常问题,我们知道很多时候我们的参数校验都是放在controller层的传入参数进行校验,我们常用的校验方式就是引入下列的jar包,在参数中添加@Validated,并对Bean对象的参数做不

Java日期时间API系列35

  通过Java日期时间API系列1Jdk7及以前的日期时间类(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.cnblogs.com%2Fxkzhangsanx%2Fp%2F12032719.html)中得知,Java8以前除了java.sql.Timestamp扩充