package com.itcast.utils

DateConverter.java

package com.itcast.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.beanutils.Converter;

public class DateConverter implements Converter {

	@SuppressWarnings("unchecked")
	public Object convert(Class clazz, Object value) {
		if(value==null || "".equals((String)value)) return null;
		if(value instanceof Date) return value;
		DateFormat convert = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return convert.parse((String) value);
		} catch (ParseException e) {
			DateFormat convert2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				return convert2.parse((String) value);
			} catch (ParseException e1) {}
		}
		return null;
	}

}



SexConverter.java


package com.itcast.utils;

import org.apache.commons.beanutils.Converter;

import com.itcast.bean.product.Sex;

public class SexConverter implements Converter {

	@SuppressWarnings("unchecked")
	public Object convert(Class clazz, Object value) {
		if(value==null || "".equals((String)value)) return null;
		if(value instanceof Sex) return value;
		try{
			return Sex.valueOf((String) value);
		}catch (Exception e) {}
		return null;
	}

}



SystemPrivilegePKConverter.java



package com.itcast.utils;

import org.apache.commons.beanutils.Converter;

import com.itcast.bean.privilege.SystemPrivilegePK;

public class SystemPrivilegePKConverter implements Converter {

	public Object convert(Class arg0, Object value) {
		if(value==null || "".equals((String)value)) return null;
		if(value instanceof SystemPrivilegePK) return value;
		try {
			String idstr = (String)value;
			String[] idkey = idstr.split(",");
			if(idkey.length==2){
				return new SystemPrivilegePK(idkey[0], idkey[1]);
			}
		}catch (Exception e) {}
		return null;
	}

}

你可能感兴趣的:(java,apache,bean)