CommonUtils.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package com.xzl.web.utils;
  2. import org.apache.commons.lang3.StringUtils;
  3. import java.beans.BeanInfo;
  4. import java.beans.IntrospectionException;
  5. import java.beans.Introspector;
  6. import java.beans.PropertyDescriptor;
  7. import java.lang.reflect.InvocationTargetException;
  8. import java.lang.reflect.Method;
  9. import java.text.SimpleDateFormat;
  10. import java.util.*;
  11. /**
  12. * @Description:
  13. * @Author: gl
  14. * @CreateDate: 2018/9/4 11:32
  15. * @UpdateUser:
  16. * @UpdateDate: 2018/9/4 11:32
  17. * @UpdateRemark:
  18. * @Version: 1.0
  19. */
  20. public class CommonUtils {
  21. public static void Copy(Object source, Object dest) throws Exception {
  22. // 获取属性
  23. BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(), Object.class);
  24. PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors();
  25. BeanInfo destBean = Introspector.getBeanInfo(dest.getClass(), Object.class);
  26. PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors();
  27. try {
  28. for (int i = 0; i < sourceProperty.length; i++) {
  29. for (int j = 0; j < destProperty.length; j++) {
  30. if (sourceProperty[i].getName().equals(destProperty[j].getName()) && sourceProperty[i].getPropertyType() == destProperty[j].getPropertyType()) {
  31. // 调用source的getter方法和dest的setter方法
  32. destProperty[j].getWriteMethod().invoke(dest, sourceProperty[i].getReadMethod().invoke(source));
  33. break;
  34. }
  35. }
  36. }
  37. } catch (Exception e) {
  38. throw new Exception("属性复制失败:" + e.getMessage());
  39. }
  40. }
  41. public static String frontCompWithZore(int sourceDate, int formatLength) {
  42. /*
  43. * 0 指前面补充零
  44. * formatLength 字符总长度为 formatLength
  45. * d 代表为正数。
  46. */
  47. String newString = String.format("%0" + formatLength + "d", sourceDate);
  48. return newString;
  49. }
  50. /*将一个 JavaBean 对象转化为一个 Map*/
  51. @SuppressWarnings({"rawtypes", "unchecked"})
  52. public static Map convertBean(Object bean)
  53. throws IntrospectionException, IllegalAccessException, InvocationTargetException {
  54. Class type = bean.getClass();
  55. Map returnMap = new HashMap();
  56. BeanInfo beanInfo = Introspector.getBeanInfo(type);
  57. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  58. for (int i = 0; i < propertyDescriptors.length; i++) {
  59. PropertyDescriptor descriptor = propertyDescriptors[i];
  60. String propertyName = descriptor.getName();
  61. if (!propertyName.equals("class")) {
  62. Method readMethod = descriptor.getReadMethod();
  63. Object result = readMethod.invoke(bean, new Object[0]);
  64. if (result != null) {
  65. returnMap.put(propertyName, result);
  66. } else {
  67. returnMap.put(propertyName, "");
  68. }
  69. }
  70. }
  71. return returnMap;
  72. }
  73. /*将一个 Map 对象转化为一个 JavaBean*/
  74. @SuppressWarnings("rawtypes")
  75. public static Object convertMap(Class type, Map map)
  76. throws IntrospectionException, IllegalAccessException,
  77. InstantiationException, InvocationTargetException {
  78. BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
  79. Object obj = type.newInstance(); // 创建 JavaBean 对象
  80. // 给 JavaBean 对象的属性赋值
  81. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  82. for (int i = 0; i < propertyDescriptors.length; i++) {
  83. PropertyDescriptor descriptor = propertyDescriptors[i];
  84. String propertyName = descriptor.getName();
  85. if (map.containsKey(propertyName)) {
  86. // 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。
  87. Object value = map.get(propertyName);
  88. Object[] args = new Object[1];
  89. args[0] = value;
  90. descriptor.getWriteMethod().invoke(obj, args);
  91. }
  92. }
  93. return obj;
  94. }
  95. //讲map中值为“”转为null
  96. public static Map<String, Object> emptyToNull(Map<String, Object> map) {
  97. Set<String> set = map.keySet();
  98. if (set != null && !set.isEmpty()) {
  99. for (String key : set) {
  100. if (map.get(key) == "") {
  101. map.put(key, null);
  102. }
  103. }
  104. }
  105. return map;
  106. }
  107. //生成订单号
  108. public static String generatOrderNo(String preOrder, String orderNo) {
  109. if (StringUtils.isEmpty(orderNo)) {
  110. return preOrder + getCurrDate() + "0001";
  111. } else {
  112. int i = Integer.parseInt(orderNo);
  113. i++;
  114. String num = String.valueOf(i);
  115. return preOrder + getCurrDate() + numString(num);
  116. }
  117. }
  118. public static String numString(String str) {//递归生成后五位字符串
  119. if (str.length() == 4) {
  120. return str;
  121. } else {
  122. return numString("0" + str);
  123. }
  124. }
  125. public static String getCurrDate() {
  126. SimpleDateFormat sf = new SimpleDateFormat("yyMMdd");
  127. String date = sf.format(new Date());
  128. return date;
  129. }
  130. public static Date addDays(Date date, int amount) {
  131. Calendar now = Calendar.getInstance();
  132. now.setTime(date);
  133. now.set(Calendar.DATE, now.get(Calendar.DATE) + amount);
  134. return now.getTime();
  135. }
  136. }