分享给大家10个我日常使用多的工具类
1、过滤emoji表情符
public static String filterEmoji(String source) {
String emojiString = "[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]";//emoj正则
if(!StringUtil.isStrBlank(source)){
Pattern emoji = Pattern.compile (emojiString,Pattern.UNICODE_CASE | Pattern . CASE_INSENSITIVE ) ;
Matcher emojiMatcher = emoji.matcher(source);
if (emojiMatcher.find()){
source = emojiMatcher.replaceAll("*");
return source ;
}
}
return source;
}
2、判断字符串中是否包含 汉字
public static boolean isContainChinese(String str) {
if (!StringUtil.isStrBlank(str)) {
for (int i = 0; i < str.length(); i++) {
if (Character.toString(str.charAt(i)).matches(
"[\\u4E00-\\u9FA5]+")) {
return true;
}
}
}
return false;
}
3、计算年龄,仅用年计算
public static int getAgeByBirthday(Date birthday) {
Calendar cal = Calendar.getInstance();
if (birthday == null || cal.before(birthday)) {
return 0;
}
// 获取当前年
int yearNow = cal.get(Calendar.YEAR);
// 获取生日年
cal.setTime(birthday);
int yearBirth = cal.get(Calendar.YEAR);
// 计算年龄
int age = yearNow - yearBirth;
return age;
}
4、根据身份证计算生日
/**
*
* <pre>
* 描述:根据身份证计算生日
* 作者:futian
* 时间:2017年4月21日上午10:14:23
* </pre>
* @throws BaseBllException
*/
public static Date getBirthDayByIdNo(String idNo) throws BaseBllException {
Date birth =null;
String birthDay = null;
if (StringUtil.isStrBlank(idNo)) {
return birth;
}
if (idNo.length() == 15) {
birthDay = "19" + idNo.substring(6, 12);
} else if (idNo.length() == 18) {
birthDay = idNo.substring(6, 14);
}
SimpleDateFormat sfd = new SimpleDateFormat("yyyyMMdd");
if(null != birthDay){
try {
birth = sfd.parse(birthDay);
} catch (ParseException e) {
throw new BaseBllException("生日转换出错");
}
}
return birth;
}
5、根据身份证获取性别
public static int getAgeByIdNo(String idNo){
int sex =1;
if (StringUtil.isStrBlank(idNo)) {
return sex;
}
int length = idNo.length();
String sexFlag = idNo.charAt(length - 2) + "";
//1:男 2:女
if (Integer.parseInt(sexFlag) % 2 != 0) {
sex = 1;
} else {
sex = 2;
}
return sex;
}
6、将List<String> 转换为 字符串
public static String listToString(List<String> stringList){
if (stringList==null||stringList.size()==0) {
return "";
}
StringBuilder result=new StringBuilder();
boolean flag=false;
for (String string : stringList) {
if(string!=null&&!"".equals(string)){
if (flag) {
result.append(",");
}else {
flag=true;
}
result.append(string);
}
}
return result.toString();
}
7、将List<String> 转换为sql In的参数 字符串
/**
* <pre>
* 描述:将List<String> 转换为sql In的参数 字符串
* 时间:2015年5月25日下午2:19:38
* @param stringList
* @return
* returnType:String
*/
public static String listToSqlIn(List<String> stringList){
if (stringList==null||stringList.size()==0) {
return null;
}
StringBuilder result=new StringBuilder();
boolean flag=false;
for (String string : stringList) {
if(string!=null&&!"".equals(string)){
if (flag) {
result.append(",");
}else {
flag=true;
}
result.append("'"+string.trim()+"'");
}
}
return result.toString();
}
8、获取今天之前或之后count天的时间
/**
* <pre>
* 描述:获取今天之前或之后count天的时间
* 作者:futian
* 时间:2016年11月3日上午11:05:51
* @param count
* @return
* returnType:String
* </pre>
*/
public static String getTimeBeforeOrAfterToday(int count) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, count);
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
}
9、获得指定日期的后一天
/**
* 获得指定日期的后一天
* @param specifiedDay
* @return
*/
public static String getSpecifiedDayAfter(String specifiedDay,String format) {
String dateString = "";
if (StringUtil.isStrBlank(specifiedDay)) {
return dateString;
}
if (StringUtil.isStrBlank(format)) {
format = "yyyy-MM-dd";
}
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat(format).parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day + 1);
String dayAfter = new SimpleDateFormat(format).format(c.getTime());
return dayAfter;
}
10、手机号校验
public static boolean isPhoneNumber(String phone) {
Pattern pattern = Pattern.compile("^(\\+86)?1[3|5|4|7|8]\\d{9}$");
Matcher m = pattern.matcher(phone);
if (m.matches()) {
return true;
} else {
return false;
}
}