java常用逻辑判断工具类
1、对象是否为空
2、对象是否不为空
3、对象组中是否存在
4、对象组中是否全是 Empty Object
5、判断map里的该键值是否为空
6、判断是否为时间戳
7、手机号验证
8、18位身份证获取性别和年龄
9、15位身份证获取性别和年龄
package com.zjsos.sostool.common.util;
import cn.hutool.core.util.ObjectUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LogicUtil {
/**
* 对象是否为空
*
* @param o
* @return
*/
@SuppressWarnings("rawtypes")
public static boolean isEmpty(Object o) {
if (null == o) {
return true;
}
if (o instanceof List) {
List list = (List) o;
if (list.size() > 0) {
return false;
} else {
return true;
}
} else if (o instanceof Object[]) {
Object[] objs = (Object[]) o;
if (objs.length > 0) {
return false;
} else {
return true;
}
} else if (o instanceof String) {
//空判断添加 null 和undefind
if (StringUtil.isBlank(o.toString())|| ObjectUtil.equal("null",o)||ObjectUtil.equal("undefined",o)) {
return true;
}
} else if (o instanceof Map) {
if (((Map) o).size() == 0) {
return true;
}
} else if (o instanceof Object[]) {
if (((Object[]) o).length == 0) {
return true;
}
} else if (o instanceof int[]) {
if (((int[]) o).length == 0) {
return true;
}
} else if (o instanceof long[]) {
if (((long[]) o).length == 0) {
return true;
}
}
return false;
}
/**
* 对象是否不为空(新增)
*
* @param o String,List,Map,Object[],int[],long[]
* @return
*/
public static boolean isNotEmpty(Object o) {
return !isEmpty(o);
}
/**
* 对象组中是否存在 Empty Object
*
* @param os 对象组
* @return
*/
public static boolean isOneEmpty(Object... os) {
for (Object o : os) {
if (isEmpty(o)) {
return true;
}
}
return false;
}
/**
* 对象组中是否全是 Empty Object
*
* @param os
* @return
*/
public static boolean isAllEmpty(Object... os) {
for (Object o : os) {
if (!isEmpty(o)) {
return false;
}
}
return true;
}
public static boolean isListEmpty(Object obj) {
List<Object> ol = (List<Object>) obj;
for (Object o : ol) {
if (!isEmpty(o)) {
return false;
}
}
return true;
}
/**
* @throws
* @Description: TODO 判断map里的该键值是否为空
* @Title: isEmpty
* @author xqy
* @date 2018年6月4日 上午11:21:05
* @param: @param params
* @param: @param key
* @param: @return
* @return: boolean
*/
public static boolean isEmpty(Map<String, Object> params, String key) {
if (!params.containsKey(key) || isEmpty(params.get(key))) {
return true;
}
return false;
}
/**
* 判断是否为时间戳
*
* @throws ParseException
* @Author sharlot2050@foxmail.com
* @Date 2019年4月4日
* @Version 1.00
*/
public static boolean isTimestamp(String timestamp) {
long d = 0L;
long ds = 4102416000000L;
long n = -1;
try {
n = Long.parseLong(timestamp);
} catch (NumberFormatException e) {
n = -1;
}
return n > d && n < ds;
}
/**
* 手机号验证
*
* @Author sharlot2050@foxmail.com
* @Date 2019年4月4日
* @Version 1.00
*/
public static boolean isPhone(String phone) {
String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
if (isEmpty(phone) || phone.length() != 11) {
return false;
} else {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(phone);
boolean isMatch = m.matches();
return isMatch;
}
}
/**
* source = {"name": ""}
* vpairs = {"name": "姓名不能为空"}
*
* @throws Exception
* @Author sharlot2050@foxmail.com
* @Date 2019年4月4日
* @Version 1.00
*/
public static void validateByMap(Map<String, Object> source, Map<String, String> vpairs) throws Exception {
for (String key : vpairs.keySet()) {
if (LogicUtil.isEmpty(source.get(key))) throw new Exception(vpairs.get(key));
}
}
/**
* @Description AssertUtil.isNotNull
* @Author sharlot2050@foxmail.com
* @date 2020/6/9
*/
public static void isEmptyMsg(Object o, String msg) {
if (isEmpty(o)) {
throw new IllegalArgumentException(msg);
}
}
/**
* 18位身份证获取性别和年龄
*
* @return
* @throws Exception
*/
public static Map<String, Object> identityCard(String sfz) {
try {
if (isEmpty(sfz)) throw new Exception("sfz不能为空");
if (sfz.length() == 18) {
return identityCard18(sfz);
} else if (sfz.length() == 15) {
return identityCard15(sfz);
} else {
throw new Exception("aaa");
}
} catch (Exception e) {
// e.printStackTrace();
Map<String, Object> map = new HashMap<String, Object>();
map.put("sex", "未知");
map.put("age", "未知");
return map;
}
}
private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
public static Map<String, Object> identityCard18(String CardCode) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
// 得到年份
String year = CardCode.substring(6).substring(0, 4);
// 得到月份
String month = CardCode.substring(10).substring(0, 2);
//得到日
//String day=CardCode.substring(12).substring(0,2);
String sex;
// 判断性别
if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {
sex = "女";
} else {
sex = "男";
}
// 得到当前的系统时间
Date date = new Date();
// 当前年份
String currentYear = format.format(date).substring(0, 4);
// 月份
String currentMonth = format.format(date).substring(5, 7);
//String currentdDay=format.format(date).substring(8,10);
int age = 0;
// 当前月份大于用户出身的月份表示已过生日
if (Integer.parseInt(month) <= Integer.parseInt(currentMonth)) {
age = Integer.parseInt(currentYear) - Integer.parseInt(year) + 1;
} else {
// 当前用户还没过生日
age = Integer.parseInt(currentYear) - Integer.parseInt(year);
}
map.put("sex", sex);
map.put("age", age);
return map;
}
/**
* 15位身份证获取性别和年龄
*
* @param card
* @return
* @throws Exception
*/
public static Map<String, Object> identityCard15(String card) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
//年份
String year = "19" + card.substring(6, 8);
//月份
String yue = card.substring(8, 10);
//日
//String day=card.substring(10, 12);
String sex;
if (Integer.parseInt(card.substring(14, 15)) % 2 == 0) {
sex = "女";
} else {
sex = "男";
}
// 得到当前的系统时间
Date date = new Date();
//当前年份
String currentYear = format.format(date).substring(0, 4);
//月份
String currentMonth = format.format(date).substring(5, 7);
//String fday=format.format(date).substring(8,10);
int age = 0;
//当前月份大于用户出身的月份表示已过生日
if (Integer.parseInt(yue) <= Integer.parseInt(currentMonth)) {
age = Integer.parseInt(currentYear) - Integer.parseInt(year) + 1;
} else {
// 当前用户还没过生日
age = Integer.parseInt(currentYear) - Integer.parseInt(year);
}
map.put("sex", sex);
map.put("age", age);
return map;
}
}