java如何填充对象中的时间属性呢?
前提:不知道对象具体类型.
解决方法:使用反射
?
/*** * 把对象中的列,类型为时间的都设置为当前时间 * @param obj * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static void fillTimeForObj(Object obj) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{ List<Field> fieldsList=getAllFieldList(obj.getClass()); int size=fieldsList.size(); for(int i=0;i<size;i++){ Field f=fieldsList.get(i); String typeName=f.getType().getName(); if(typeName.equals("java.sql.Timestamp")||typeName.equals("java.util.Date")){ setObjectValue(obj, f, TimeHWUtil.getCurrentTimestamp()); }else if(typeName.equals("java.sql.Date")){ setObjectValue(obj, f, new java.util.Date()); } } }
?依赖的方法:
?
?
/*** * get all field ,including fields in father/super class * * @param clazz * @return */ public static List<Field> getAllFieldList(Class<?> clazz) { List<Field> fieldsList = new ArrayList<Field>();// return object if (clazz == null) { return null; } Class<?> superClass = clazz.getSuperclass();// father class if (!superClass.getName().equals(Object.class.getName()))/* * java.lang.Object */{ // System.out.println("has father"); fieldsList.addAll(getAllFieldList(superClass));// Recursive } Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; // 排除因实现Serializable 接口而产生的属性serialVersionUID if (!field.getName().equals("serialVersionUID")) { fieldsList.add(field); } } return fieldsList; } /*** * 设置对象的属性值。 * * @param obj * @param propertyName * : property name * @param propertyValue * : value of property<br> must be String or Field * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static void setObjectValue(Object obj, Object propertyName, Object propertyValue) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { if (ValueWidget.isNullOrEmpty(propertyName) || ValueWidget.isNullOrEmpty (propertyValue)) { return; } Class<?> clazz = obj.getClass(); Field name = null; if(propertyName instanceof String){ name=getSpecifiedField(clazz, (String)propertyName); }else{ name=(Field)propertyName; } name.setAccessible(true); name.set(obj, propertyValue); } /*** * Get Specified Field * * @param clazz * @param fieldName * @return */ public static Field getSpecifiedField(Class<?> clazz, String fieldName) { Field f = null; if (ValueWidget.isNullOrEmpty(clazz)) { return null; } try { f = clazz.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { return getSpecifiedField(clazz.getSuperclass()/* * may be null if it is * Object . */, fieldName); // e.printStackTrace(); } return f; }
?
把A对象的时间属性的值设置到B对象中
/*** * 使用spring MVC保存对象时,对象的createTime(时间类型的属性)没有注入进来, * <br>这时需要后台先通过id获取持久化对象,然后手动注入 * @param editedObj * @param persistentObj * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static void fillTimeForEditedObj(Object editedObj,Object persistentObj) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{ if(!editedObj.getClass().getName().equals(persistentObj.getClass().getName())){ throw new RuntimeException("Two object type should be the same ,but is different ."); } List<Field> fieldsList=getAllFieldList(editedObj.getClass()); int size=fieldsList.size(); for(int i=0;i<size;i++){ Field f=fieldsList.get(i); String typeName=f.getType().getName(); if(editedObj instanceof java.util.Date || typeName.equals("java.sql.Timestamp")||typeName.equals("java.util.Date")||typeName.equals("java.sql.Date")){ Object valueEdited=getObjectValue(editedObj, f); Object valuePersistent=getObjectValue(persistentObj, f); if(ValueWidget.isNullOrEmpty(valueEdited)&&(!ValueWidget.isNullOrEmpty(valuePersistent))){ setObjectValue(editedObj, f, valuePersistent); } } } }
?参考:
?
原文:http://hw1287789687.iteye.com/blog/2154705