填充对象中的时间属性

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);
				}
				
			}
		}
	}

 参考:

Java 反射常用方法

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值