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}
java邮箱校验,手机号校验,格式化日期等等
Wesley13
2021-10-11
1164 1 0
点赞
收藏
评论区
加载中...