首页 > 代码库 > CommonUtils
CommonUtils
package com.utils;import java.lang.reflect.Field;import java.math.BigDecimal;import java.sql.Timestamp;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Map;import java.util.Set;public class CommonUtils { /** * 将map中的值赋值到po中 * * @param map * 前台传过来的值封装成的map对象 * @param obj * po实体类 */ public static void getLatestObjectByMap(Map<String, ?> map, Object obj) { Set<String> keys = map.keySet(); for (String key : keys) { Object value = map.get(key); Class<? extends Object> clazz = obj.getClass(); try { Field field = clazz.getDeclaredField(key); field.setAccessible(true); field.set(obj, getValue(key, field.getType().getName(), value)); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } /** * 将fieldValue转为dataType类型的值 * * @param key * @param dataType * @param fieldValue * @return */ private static Object getValue(String key, String dataType, Object fieldValue) { String errorMessage = key + "为[" + dataType + "]而设置的值为[" + fieldValue.getClass().getName() + "]"; String numberFormatErrorMessage = key + " [" + fieldValue + "]转换为[" + dataType + "]出错"; System.out.println(dataType); if ("java.sql.Date".equals(dataType)) { if (fieldValue instanceof java.sql.Date) { return fieldValue; } else if (fieldValue instanceof java.lang.String) { String formatString = "yyyy-MM-dd"; try { SimpleDateFormat sdf = new SimpleDateFormat(formatString); Date date = sdf.parse((String) fieldValue); return new java.sql.Date(date.getTime()); } catch (Exception e) { throw new RuntimeException(key + "[" + fieldValue + "]格式错误,正确格式为[" + formatString + "]"); } } else { throw new RuntimeException(errorMessage); } } else if ("java.util.Date".equals(dataType)) { if (fieldValue instanceof java.util.Date) { return fieldValue; } else if (fieldValue instanceof java.lang.String) { String formatString = "yyyy-MM-dd"; try { SimpleDateFormat sdf = new SimpleDateFormat(formatString); java.util.Date date = sdf.parse((String) fieldValue); return date; } catch (Exception e) { throw new RuntimeException(key + "[" + fieldValue + "]格式错误,正确格式为[" + formatString + "]"); } } else { throw new RuntimeException(errorMessage); } } else if ("java.sql.Timestamp".equals(dataType)) { if (fieldValue instanceof java.sql.Timestamp) { return fieldValue; } else if (fieldValue instanceof java.lang.String) { String formatString = "yyyy-MM-dd HH:mm:ss"; try { SimpleDateFormat sdf = new SimpleDateFormat(formatString); Date date = sdf.parse((String) fieldValue); return new Timestamp(date.getTime()); } catch (Exception e) { throw new RuntimeException(key + "[" + fieldValue + "]格式错误,正确格式为[" + formatString + "]"); } } else { throw new RuntimeException(errorMessage); } } else if ("java.lang.String".equals(dataType)) { if (fieldValue instanceof java.lang.String) { return fieldValue; } else { throw new RuntimeException(errorMessage); } } else if ("java.math.BigDecimal".equals(dataType)) { if (fieldValue instanceof java.math.BigDecimal) { return fieldValue; } else if (fieldValue instanceof java.lang.String) { try { return new BigDecimal(String.valueOf(fieldValue)); } catch (Exception e) { throw new RuntimeException(numberFormatErrorMessage); } } else { throw new RuntimeException(errorMessage); } } else if (("java.lang.Integer".equals(dataType)) || ("int".equals(dataType))) { if (fieldValue instanceof java.lang.Integer) { return fieldValue; } else if (fieldValue instanceof java.lang.String) { try { return new Integer(String.valueOf(fieldValue)); } catch (Exception e) { throw new RuntimeException(numberFormatErrorMessage); } } else { throw new RuntimeException(errorMessage); } } else if (("java.lang.Long".equals(dataType)) || ("long".equals(dataType))) { if (fieldValue instanceof java.lang.Long) { return fieldValue; } else if (fieldValue instanceof java.lang.String) { try { return new Long(String.valueOf(fieldValue)); } catch (Exception e) { throw new RuntimeException(numberFormatErrorMessage); } } else { throw new RuntimeException(errorMessage); } } else if (("java.lang.Short".equals(dataType)) || ("short".equals(dataType))) { if (fieldValue instanceof java.lang.Short) { return fieldValue; } else if (fieldValue instanceof java.lang.String) { try { return new Short(String.valueOf(fieldValue)); } catch (Exception e) { throw new RuntimeException(numberFormatErrorMessage); } } else { throw new RuntimeException(errorMessage); } } else if (("java.lang.Byte".equals(dataType)) || ("byte".equals(dataType))) { if (fieldValue instanceof java.lang.Byte) { return fieldValue; } else if (fieldValue instanceof java.lang.String) { try { return new Byte(String.valueOf(fieldValue)); } catch (Exception e) { throw new RuntimeException(numberFormatErrorMessage); } } else { throw new RuntimeException(errorMessage); } } else if (("java.lang.Float".equals(dataType)) || ("float".equals(dataType))) { if (fieldValue instanceof java.lang.Float) { return fieldValue; } else if (fieldValue instanceof java.lang.String) { try { return new Float(String.valueOf(fieldValue)); } catch (Exception e) { throw new RuntimeException(numberFormatErrorMessage); } } else { throw new RuntimeException(errorMessage); } } else if (("java.lang.Double".equals(dataType)) || ("double".equals(dataType))) { if (fieldValue instanceof java.lang.Double) { return fieldValue; } else if (fieldValue instanceof java.lang.String) { try { return new Double(String.valueOf(fieldValue)); } catch (Exception e) { throw new RuntimeException(numberFormatErrorMessage); } } else { throw new RuntimeException(errorMessage); } } else if (("java.lang.Boolean".equals(dataType)) || ("boolean".equals(dataType))) { if (fieldValue instanceof java.lang.Boolean) { return fieldValue; } else if (fieldValue instanceof java.lang.String) { if ("true".equals(fieldValue)) { return new Boolean(true); } else if ("false".equals(fieldValue)) { return new Boolean(false); } else { throw new RuntimeException(numberFormatErrorMessage); } } else { throw new RuntimeException(errorMessage); } } else if (("java.lang.Character".equals(dataType)) || ("char".equals(dataType))) { { return new Character(String.valueOf(fieldValue).charAt(0)); } } return fieldValue; }}
CommonUtils
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。