标签:sed 开发效率 lse sem 完全 iat warnings artifact awt
1.commons-lang.jar包下面 生成随机数
org.apache.commons.lang3.RandomStringUtils#randomNumeric
//生成一个十位的随机标识
String random = RandomStringUtils.randomNumeric(10);
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
2.日期格式化:
public static String formatDate(Date dateTime, String pattern) { if (dateTime != null && !StringUtils.isEmpty(pattern)) { DateFormat df = new SimpleDateFormat(pattern); String result = df.format(dateTime); return result; } else { return ""; } } 备注:pattern可能的取值 public static final String FORMAT_LONG0 = "yyyyMMddHHmmss"; public static final String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss"; public static final String FORMAT_LONG2 = "yyyy/MM/dd HH:mm:ss"; public static final String FORMAT_LONG3 = "yyyy.MM.dd HH:mm:ss"; public static final String FORMAT_LONG4 = "yyyy-MM-dd"; public static final String FORMAT_LONG5 = "yyyyMMddHHmmssSSS";
3.判断是否为空:
3.1字符串判断是否为空 public static boolean isEmpty(String str) { return str == null || str.length() == 0; } 3.2集合判断是否为空: commons-collections.jar下面 org.apache.commons.collections.CollectionUtils#isEmpty public static boolean isEmpty(Collection coll) { return coll == null || coll.isEmpty(); } 3.2集合map是否为空: commons-collections.jar下面 public static boolean isEmpty(Map map) { return map == null || map.isEmpty(); }
4.将Map和javaBean之间的相互转化
public static void main(String[] args) { UserDto userDto=new UserDto(); //javaBean转化为dto Map<String, Object> stringObjectMap1 = toMap(userDto); System.out.println(stringObjectMap1); Map<String, Object> stringObjectMap = new HashMap<>(); stringObjectMap.put("address","ccc"); stringObjectMap.put("userName","zjb"); stringObjectMap.put("code","11"); try { //map 转化为javaBean convertMap(userDto.getClass(),stringObjectMap); } catch (IntrospectionException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } /** * 将一个 JavaBean 对象转化为一个 Map * @param bean 要转化的JavaBean 对象 * @return 转化出来的 Map 对象 * @throws IntrospectionException 如果分析类属性失败 * @throws IllegalAccessException 如果实例化 JavaBean 失败 * @throws InvocationTargetException 如果调用属性的 setter 方法失败 */ @SuppressWarnings("rawtypes") public static Map<String,Object> toMap(Object bean) { Class<? extends Object> clazz = bean.getClass(); Map<String, Object> returnMap = new HashMap<String, Object>(); BeanInfo beanInfo = null; try { beanInfo = Introspector.getBeanInfo(clazz); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; String propertyName = descriptor.getName(); if (StringUtils.isNotEmpty(propertyName)&&!"class".equals(propertyName)) { Method readMethod = descriptor.getReadMethod(); Object result = null; result = readMethod.invoke(bean, new Object[0]); propertyName = propertyName.toString(); if (null != result) { result = result.toString(); } returnMap.put(propertyName, result); } } } catch (Exception e) { //异常日志信息捕获并打印 } return returnMap; } /** * 将一个 Map 对象转化为一个 JavaBean * @param type 要转化的类型 * @param map 包含属性值的 map * @return 转化出来的 JavaBean 对象 * @throws IntrospectionException * 如果分析类属性失败 * @throws IllegalAccessException * 如果实例化 JavaBean 失败 * @throws InstantiationException * 如果实例化 JavaBean 失败 * @throws InvocationTargetException * 如果调用属性的 setter 方法失败 */ public static Object convertMap(Class type, Map map) throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException { BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性 Object obj = type.newInstance(); // 创建 JavaBean 对象 System.out.println(obj.toString()); try{ // 给 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); } } System.out.println(obj.toString()); }catch (Exception e){ //异常日志信息捕获并打印 } return obj; }
整理上述工具类中的完全是为了巩固一下基础、提高开发效率,参考价值不大!
标签:sed 开发效率 lse sem 完全 iat warnings artifact awt
原文地址:https://www.cnblogs.com/jiarui-zjb/p/12326085.html