package com.xzl.web.utils; import org.apache.commons.lang3.StringUtils; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.*; /** * @Description: * @Author: gl * @CreateDate: 2018/9/4 11:32 * @UpdateUser: * @UpdateDate: 2018/9/4 11:32 * @UpdateRemark: * @Version: 1.0 */ public class CommonUtils { public static void Copy(Object source, Object dest) throws Exception { // 获取属性 BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(), Object.class); PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors(); BeanInfo destBean = Introspector.getBeanInfo(dest.getClass(), Object.class); PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors(); try { for (int i = 0; i < sourceProperty.length; i++) { for (int j = 0; j < destProperty.length; j++) { if (sourceProperty[i].getName().equals(destProperty[j].getName()) && sourceProperty[i].getPropertyType() == destProperty[j].getPropertyType()) { // 调用source的getter方法和dest的setter方法 destProperty[j].getWriteMethod().invoke(dest, sourceProperty[i].getReadMethod().invoke(source)); break; } } } } catch (Exception e) { throw new Exception("属性复制失败:" + e.getMessage()); } } public static String frontCompWithZore(int sourceDate, int formatLength) { /* * 0 指前面补充零 * formatLength 字符总长度为 formatLength * d 代表为正数。 */ String newString = String.format("%0" + formatLength + "d", sourceDate); return newString; } /*将一个 JavaBean 对象转化为一个 Map*/ @SuppressWarnings({"rawtypes", "unchecked"}) public static Map convertBean(Object bean) throws IntrospectionException, IllegalAccessException, InvocationTargetException { Class type = bean.getClass(); Map returnMap = new HashMap(); BeanInfo beanInfo = Introspector.getBeanInfo(type); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; String propertyName = descriptor.getName(); if (!propertyName.equals("class")) { Method readMethod = descriptor.getReadMethod(); Object result = readMethod.invoke(bean, new Object[0]); if (result != null) { returnMap.put(propertyName, result); } else { returnMap.put(propertyName, ""); } } } return returnMap; } /*将一个 Map 对象转化为一个 JavaBean*/ @SuppressWarnings("rawtypes") public static Object convertMap(Class type, Map map) throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException { BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性 Object obj = type.newInstance(); // 创建 JavaBean 对象 // 给 JavaBean 对象的属性赋值 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; String propertyName = descriptor.getName(); if (map.containsKey(propertyName)) { // 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。 Object value = map.get(propertyName); Object[] args = new Object[1]; args[0] = value; descriptor.getWriteMethod().invoke(obj, args); } } return obj; } //讲map中值为“”转为null public static Map emptyToNull(Map map) { Set set = map.keySet(); if (set != null && !set.isEmpty()) { for (String key : set) { if (map.get(key) == "") { map.put(key, null); } } } return map; } //生成订单号 public static String generatOrderNo(String preOrder, String orderNo) { if (StringUtils.isEmpty(orderNo)) { return preOrder + getCurrDate() + "0001"; } else { int i = Integer.parseInt(orderNo); i++; String num = String.valueOf(i); return preOrder + getCurrDate() + numString(num); } } public static String numString(String str) {//递归生成后五位字符串 if (str.length() == 4) { return str; } else { return numString("0" + str); } } public static String getCurrDate() { SimpleDateFormat sf = new SimpleDateFormat("yyMMdd"); String date = sf.format(new Date()); return date; } public static Date addDays(Date date, int amount) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.DATE, now.get(Calendar.DATE) + amount); return now.getTime(); } }