1. JSON操作(jackson为例)
包括:
- 将对象转换成json字符串
- 将json结果集转化为对象
- 将json数据转换成对象List
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
public class JsonUtil {
// 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 将对象转换成json字符串
* @param data 对象
* @return json字符串
*/
public static String objectToJson(Object data) {
try {
return MAPPER.writeValueAsString(data);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
/**
* 将json结果集转化为对象
* @param jsonData json结果
* @param beanType 对象
* @param <T> 泛型
* @return 目的对象
*/
public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
try {
return MAPPER.readValue(jsonData, beanType);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将json数据转换成对象list
* @param jsonData json数据
* @param beanType 对象
* @param <T> 泛型
* @return 对象list
*/
public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
try {
return MAPPER.readValue(jsonData, javaType);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
2. 对象操作
包括:
- 判断一个对象的所有属性是否为有null值
- 获取属性名数组
- 通过属性名获取属性值
- 获取属性的数据类型
public class PublicUtil {
/**
* 判断一个对象的所有属性是否为有 null 值
* @param obj 判断的对象
* @return 有null,返回false;无null,返回true
* @throws Exception
*/
public static boolean isAllFieldNull(Object obj) throws Exception{
Class stuCla = (Class) obj.getClass(); // 得到类对象
Field[] fs = stuCla.getDeclaredFields(); // 得到属性集合
boolean flag = true;
for (Field f : fs) { // 遍历属性
f.setAccessible(true); // 设置属性是可以访问的(私有的也可以)
Object val = f.get(obj); // 得到此属性的值
if(val != null) { // 只要有1个属性不为空,那么就不是所有的属性值都为空
flag = false;
break;
}
}
return flag;
}
/**
* 获取属性名数组
* @param object 目标对象
* @return 属性名数组
*/
public static String[] getFieldName(Object object){
Field[] fields = object.getClass().getDeclaredFields();
String[] fieldNames = new String[fields.length];
for(int i = 0; i < fields.length; i++){
if(fields[i].isAnnotationPresent(ExcelProperty.class)){
fieldNames[i] = fields[i].getName();
}
}
return fieldNames;
}
/**
* 通过属性名获取属性值
* 忽略大小写
* @param obj 目标对象
* @param name 属性名
* @return 属性值
*/
public static Object getFieldValue(Object obj, String name){
try {
Field[] fields = obj.getClass().getDeclaredFields();
Object object = null;
for (Field field : fields) {
field.setAccessible(true); // 可以获取到私有属性
if (field.getName().toUpperCase().equals(name.toUpperCase())) {
object = field.get(obj);
break;
}
}
return object;
}catch (Exception e) {
return false;
}
}
/**
* 获取属性的数据类型
* @param fieldName 属性名
* @param o 属性所属的对象
* @return 数据类型
*/
public static Object getFiledType(String fieldName, Object o) {
Field[] fields = o.getClass().getDeclaredFields();
for (Field field : fields) {
if (Objects.equals(fieldName, field.getName())) {
return field.getType();
}
}
return null;
}
}
3. 集合操作
包括:
- 移除List中重复的元素
- 将字符串的ID数组转化为整型List
public class PublicUtil {
/**
* 移除List中重复的元素
* @param list 原List集合
* @return 去重后的List集合
*/
public static List removeDuplicate(List list) {
Set h = new HashSet(list);
list.clear();
list.addAll(h);
return list;
}
/**
* 将字符串的ID数组转化为整型List
* @param ids 字符串ID数组
* @return 整型ID数组
*/
public static List<Integer> getIntegerIdList(String[] ids) {
List<Integer> idList = new ArrayList<>();
for (String id : ids) {
idList.add(Integer.parseInt(id));
}
return idList;
}
}
4. 字符串操作
包括:
- 判断字符串是否全为数字
- 去除字符串中的 + - .
public class PublicUtil {
/**
* 判断字符串是否全为数字
* @param str 目标字符串
* @return 包含除数字外的字符,返回false,反之
*/
public static boolean isNumberToString(String str) {
if("".equals(str)){
return false;
}
StringBuilder stringBuilder = deleteChar(str);
for (int i = stringBuilder.length(); --i >= 0; ) {
int chr = stringBuilder.charAt(i);
if (chr < 48 || chr > 57)
return false;
}
return true;
}
/**
* 去除字符串中的 + - .
* @param str 目标字符串
* @return 去除 + - . 后的字符串
*/
private static StringBuilder deleteChar(String str) {
int flag;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(str);
if (stringBuilder.charAt(0) == '+' || stringBuilder.charAt(0) == '-') {
stringBuilder.deleteCharAt(0);
}
if ((flag = stringBuilder.indexOf(".")) != -1) {
stringBuilder.deleteCharAt(flag);
}
return stringBuilder;
}
}
5. 时间操作
public class TimeUtil {
/**
* 判断当前时间是否晚于指定时间
* @param hour 时
* @param minute 分
* @return 当前时间晚于指定时间返回true,反之
*/
public static boolean isAfterTime(int hour, int minute) {
Date now = new Date();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hour); // 设置小时
cal.set(Calendar.MINUTE, minute); // 设置分钟
if (now.after(cal.getTime())) {
return true; // 当前时间晚于指定时间
}
return false;
}
}