刚刚假期归来,由于前一段时间加班了一个月,所以假期就实实在在的放松了一下,小工这个假期都快玩疯了,导致上班的时候感觉好累,你们有没有这种感觉???
精力有限,分享无限,作为码农的我们,利用有限的精力去积累工作以外的技术点,这样才能迅速的成长。最近一直在看各种各样的博客,有身边朋友的,有各个平台的,一直在寻找着出项目之外的技术点,积累分享,我们不是一个人在行走在简书的道路上,大家一起分享,一起走向技术大牛(扯的有点远了)。
tooopen_sy_202706818574.jpg
现在贴一下工作当中用到的非常实用的工具类,有了它们让你的代码看起来更加的简洁美观,让你感觉不是在在码砖,而是在进行艺术创造。。。
/**
* 电话号码验证
*
* @param str
* @return 验证通过返回true
*/
public static boolean isPhone(String str) {
Pattern p1 = null, p2 = null,p3=null;
Matcher m = null;
boolean b = false;
p1 = Pattern.compile("^[0][1-9][0-9]{1,2}[0-9]{5,10}$"); // 验证带区号的
p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$"); // 验证没有区号的
p3 = Pattern.compile("^(([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?$");//验证带区号和带“—”
if (str.length()<9&&str.length()>=7){
m = p2.matcher(str);
b = m.matches();
}else if (str.length()>9&&str.length()<13) {
m = p1.matcher(str);
b = m.matches();
}
else {
m=p3.matcher(str);
b=m.matches();
}
return b;
}
/*
*手机号码正则表达式
*/
public static boolean isMobileNO(String mobiles) {
^((13[0-9])|(15[^4,\D])|(18[0-9])|(14[5,7]))\d{8}$
Pattern p = Pattern.compile("^1([358]\\d|4[57]|7[^249])\\d{8}$");
Matcher m = p.matcher(mobiles);
return true ;
}
/**
* 验证邮箱
*
* @param email
* @return
*/
public static boolean isEmail(String email) {
boolean flag = false;
try {
String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(email);
flag = matcher.matches();
} catch (Exception e) {
flag = false;
}
return flag;
}
输入框emojit表情判断,参数是string类型的字符串
/** * 检测是否有emoji表情 * @param source * @return */
public static boolean containsEmoji(String source) {
int len = source.length();
for (int i = 0; i < len; i++) {
char codePoint = source.charAt(i);
if (!isEmojiCharacter(codePoint)) { //如果不能匹配,则该字符是Emoji表情
return true;
}
}
return false;
}
去除"-",参数是string类型的字符串
public static String getValues(String str){
String[] s = str.split("-");
String value = "";
for (int i=0;i<s.length;i++){
value += s[i];
}
return value;
}
/*将字符串转为时间戳*/
public static long getStringToDate(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
Date date = new Date();
try {
date = sdf.parse(time);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date.getTime() / 1000;
}
日期时间显示为星期
//获取时间戳转化为时间按星期显示,参数是long类型的时间戳
public static String getInformDate(long time) {
String date = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String min = sdf.format(new Date(time * 1000));
long hour = time / 3600 / 24;
int day = (int) hour % 7;
String week = "";
switch (day) {
case 0:
week = "星期五";
break;
case 1:
week = "星期六";
break;
case 2:
week = "星期日";
break;
case 3:
week = "星期一";
break;
case 4:
week = "星期二";
break;
case 5:
week = "星期三";
break;
case 6:
week = "星期四";
break;
default:
break;
}
date = week + " " + min;
return date;
}
//根据时间戳计算显示不同的日期时间,参数是long类型的时间戳
public static String GetGoodTime2(long time) {
String ti;
Date curentDate = new Date();
Date myDate = new Date(time);
SimpleDateFormat dfs = new SimpleDateFormat("dd");
String nowday=dfs.format(curentDate);
String lastday=dfs.format(myDate);
long subTime = (curentDate.getTime() - myDate.getTime())/1000;
if (subTime<60){
ti="刚刚";
}else if (subTime<60*60){
ti=(subTime/60)+"分钟前";
}else if (subTime <60 * 60*24&&Integer.parseInt(nowday)==Integer.parseInt(lastday)){
SimpleDateFormat df = new SimpleDateFormat("HH:mm");
ti = "今天" + df.format(myDate);
}
else if (subTime <60 * 60 * 24*2&&Integer.parseInt(nowday)!=Integer.parseInt(lastday)) {
if (Integer.parseInt(nowday)-Integer.parseInt(lastday)==1||Integer.parseInt(lastday)-Integer.parseInt(nowday)>10||Integer.parseInt(nowday)==1){
SimpleDateFormat df = new SimpleDateFormat("HH:mm");
ti = "昨天" + df.format(myDate);
}else {
SimpleDateFormat df = new SimpleDateFormat("MM-dd HH:mm");
ti=df.format(myDate);
}
}else if (subTime<60*60*24*365){
SimpleDateFormat df = new SimpleDateFormat("MM-dd HH:mm");
ti=df.format(myDate);
}else {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
ti = df.format(myDate);
}
return ti;
}
目前最全最严格的身份证验证,参数是String类型的身份证号字符串
/**
* 身份证号码正则表达式
*/
public static boolean isIDCard(String idNum) throws ParseException {
String[] ValCodeArr = { "1", "0", "x", "9", "8", "7", "6", "5", "4",
"3", "2" };
String[] Wi = { "7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7",
"9", "10", "5", "8", "4", "2" };
String Ai = "";
// ================ 号码的长度 15位或18位 ================
if (idNum.length() != 15 && idNum.length() != 18) {
return false;
}
// =======================(end)========================
// ================ 数字 除最后以为都为数字 ================
if (idNum.length() == 18) {
Ai = idNum.substring(0, 17);
} else if (idNum.length() == 15) {
Ai = idNum.substring(0, 6) + "19" + idNum.substring(6, 15);
}
if (isNumeric(Ai) == false) {
return false;
}
// =======================(end)========================
// ================ 出生年月是否有效 ================
String strYear = Ai.substring(6, 10);// 年份
String strMonth = Ai.substring(10, 12);// 月份
String strDay = Ai.substring(12, 14);// 月份
if (isDataFormat(strYear + "-" + strMonth + "-" + strDay) == false) {
return false;
}
GregorianCalendar gc = new GregorianCalendar();
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150
|| (gc.getTime().getTime() - s.parse(
strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) {
return false;
}
if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) {
return false;
}
if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) {
return false;
}
// =====================(end)=====================
// ================ 地区码时候有效 ================
Hashtable h = GetAreaCode();
if (h.get(Ai.substring(0, 2)) == null) {
return false;
}
// ==============================================
// ================ 判断最后一位的值 ================
int TotalmulAiWi = 0;
for (int i = 0; i < 17; i++) {
TotalmulAiWi = TotalmulAiWi
+ Integer.parseInt(String.valueOf(Ai.charAt(i)))
* Integer.parseInt(Wi[i]);
}
int modValue = TotalmulAiWi % 11;
String strVerifyCode = ValCodeArr[modValue];
Ai = Ai + strVerifyCode;
if (idNum.length() == 18) {
if (Ai.equalsIgnoreCase(idNum) == false) {
return false;
}
} else {
return true;
}
return true;
}
/**
* 功能:判断字符串是否为数字
* @param str
* @return
*/
public static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if (isNum.matches()) {
return true;
} else {
return false;
}
}
/**
* 功能:设置地区编码
* @return Hashtable 对象
*/
private static Hashtable GetAreaCode() {
Hashtable hashtable = new Hashtable();
hashtable.put("11", "北京");
hashtable.put("12", "天津");
hashtable.put("13", "河北");
hashtable.put("14", "山西");
hashtable.put("15", "内蒙古");
hashtable.put("21", "辽宁");
hashtable.put("22", "吉林");
hashtable.put("23", "黑龙江");
hashtable.put("31", "上海");
hashtable.put("32", "江苏");
hashtable.put("33", "浙江");
hashtable.put("34", "安徽");
hashtable.put("35", "福建");
hashtable.put("36", "江西");
hashtable.put("37", "山东");
hashtable.put("41", "河南");
hashtable.put("42", "湖北");
hashtable.put("43", "湖南");
hashtable.put("44", "广东");
hashtable.put("45", "广西");
hashtable.put("46", "海南");
hashtable.put("50", "重庆");
hashtable.put("51", "四川");
hashtable.put("52", "贵州");
hashtable.put("53", "云南");
hashtable.put("54", "西藏");
hashtable.put("61", "陕西");
hashtable.put("62", "甘肃");
hashtable.put("63", "青海");
hashtable.put("64", "宁夏");
hashtable.put("65", "新疆");
hashtable.put("71", "台湾");
hashtable.put("81", "香港");
hashtable.put("82", "澳门");
hashtable.put("91", "国外");
return hashtable;
}
/**验证日期字符串是否是YYYY-MM-DD格式
* @param str
* @return
*/
public static boolean isDataFormat(String str){
boolean flag=false;
//String regxStr="[1-9][0-9]{3}-[0-1][0-2]-((0[1-9])|([12][0-9])|(3[01]))";
String regxStr="^((\\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]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";
Pattern pattern1=Pattern.compile(regxStr);
Matcher isNo=pattern1.matcher(str);
if(isNo.matches()){
flag=true;
}
return flag;
}
//具体调用方法
![RW11M4KL2B
FG95]ZQNF{K.png](https://img.haomeiwen.com/i5362532/5867a883478d1ac1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
//显示数据时防止字符串为空而导致程序崩溃的方法
public static String value(String object, String... show) {
String returnShow = "";
if (show != null && show.length > 0) {
returnShow = show[0];
}
if (object == null || object.length() == 0)
return returnShow;
else
return object;
}
//具体调用方法
VWJ5@J6}RQ2)SMA702NWY{2.png
其他的电话号码,手机号码,字符串等用法和这个类似
/**
* 字符串转换成日期(字符串的格式为yyyy-MM-dd)
*
* @param str//日期字符串
* @return date
*/
public static Date StrToDate(String str) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
目前就整理了这些,后面遇到了再继续补充,如果大家有好的工具类,希望帮助补充。。。
网友评论