package utils;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import pojo.Point;
public class PropertiesUtil {
private static PropertyDescriptor[] getPropertyDescriptors(Object obj) {
BeanInfo ObjInfo;
try {
ObjInfo = Introspector.getBeanInfo(obj.getClass());
} catch (IntrospectionException e) {
return new PropertyDescriptor[0];
}
PropertyDescriptor[] propertyDesc = ObjInfo.getPropertyDescriptors();
return propertyDesc;
}
public static void mapToPojo(Map map, Object obj) {
PropertyDescriptor[] propertyDesc = getPropertyDescriptors(obj);
for (int i = 0; i < propertyDesc.length; i++) {
if (propertyDesc[i].getName().compareToIgnoreCase("class") == 0)
continue;
String strValue = (String) map.get(propertyDesc[i].getName());
Object value = parseObject(strValue, propertyDesc[i]
.getPropertyType());
try {
propertyDesc[i].getWriteMethod().invoke(obj, value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void pojoToMap(Object obj, Map map) {
PropertyDescriptor[] propertyDesc = getPropertyDescriptors(obj);
for (int i = 0; i < propertyDesc.length; i++) {
if (propertyDesc[i].getName().compareToIgnoreCase("class") == 0)
continue;
String key = propertyDesc[i].getName();
try {
map.put(propertyDesc[i].getName(), propertyDesc[i]
.getReadMethod().invoke(obj));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static Object parseObject(String fromObj, Class toClass) {
Object objReturn = null;
if (fromObj.equals("")) {
return objReturn;
}
String toClassName = toClass.getName();
if (fromObj.getClass().getName().equals(toClassName)) {
objReturn = fromObj;
} else if ("java.util.Date".equals(toClassName)) {
objReturn = JavaBeanDateUtil.toDate(fromObj);
} else if ("java.util.Calendar".equals(toClassName)) {
objReturn = JavaBeanDateUtil.toCalendar(fromObj);
} else if ("java.sql.Date".equals(toClassName)) {
objReturn = new Date(JavaBeanDateUtil.toDate(fromObj).getTime());
} else if ("java.sql.Time".equals(toClassName)) {
objReturn = new Timestamp(JavaBeanDateUtil.toDate(fromObj)
.getTime());
} else if ("java.sql.Timestamp".equals(toClassName)) {
objReturn = new Timestamp(JavaBeanDateUtil.toDate(fromObj,
"yyyy-MM-dd HH:mm:ss.SSS").getTime());
} else if ("java.lang.Integer".equals(toClassName)
|| "java.lang.Long".equals(toClassName)
|| "java.lang.Double".equals(toClassName)
|| "java.lang.Float".equals(toClassName)
|| "java.math.BigDecimal".equals(toClassName)
|| "java.lang.Short".equals(toClassName)) {
try {
Constructor constructor = toClass
.getConstructor(new Class[] { java.lang.String.class });
objReturn = constructor.newInstance(new Object[] { fromObj });
} catch (Exception e) {
e.printStackTrace();
}
} else {
throw new IllegalArgumentException("toClassName= " + toClassName
+ ", unsupported");
}
return objReturn;
}
public static String fieldToDbColum(String field) {
StringBuilder toField = new StringBuilder("");
for (int i = 0; i < field.length(); i++) {
char c = field.charAt(i);
if ('A' <= c && c <= 'Z') {
toField.append("_").append(String.valueOf(c).toLowerCase());
} else
toField.append(c);
}
return toField.toString();
/*
StringBuilder toField = new StringBuilder(field);
int j = 0;
for (int i = 0; i < field.length(); i++) {
char c = field.charAt(i);
if ('A' <= c && c <= 'Z') {
toField.insert(i + j, "_");
j++;
}
}
return toField.toString().toLowerCase();
*/
}
public static void main(String[] args) throws Exception {
Map map = new HashMap();
map.put("x", "100");
map.put("y", "100");
Point p = new Point();
mapToPojo(map, p);
System.out.println("横坐标:" + p.getX() + "\t纵坐标:" + p.getY());
Map map2 = new HashMap();
pojoToMap(p, map2);
for (Iterator iterator = map2.entrySet().iterator(); iterator.hasNext();) {
Entry en = (Entry) iterator.next();
System.out.println(en.getKey() + "=" + en.getValue());
}
String field = "aaaBbbbbCc";
System.out.println(fieldToDbColum(field));
}
}
public class JavaBeanDateUtil {
public static final String defaultDatePattern = "yyyy-MM-dd";
public static final String defaultDateTimePattern = "yyyy-MM-dd HH:mm:ss";
public static final String defaultTimestampPattern = "yyyy-MM-dd HH:mm:ss.SSS";
public JavaBeanDateUtil() {
}
public static String dateFormat(Object dateObj) {
return dateFormat(dateObj, null);
}
public static String dateFormat(Object dateObj, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern == null ? defaultDatePattern : pattern, Locale.CHINA);
if (dateObj == null) {
return null;
} else if (dateObj instanceof Date || dateObj instanceof java.util.Date) {
java.util.Date date = new java.util.Date(((Date)dateObj).getTime());
return sdf.format(date);
} else if (dateObj instanceof Calendar) {
java.util.Date date = ((Calendar)dateObj).getTime();
return sdf.format(date);
} else if (dateObj instanceof Time) {
java.util.Date date = new java.util.Date(((Time)dateObj).getTime());
return sdf.format(date);
} else if (dateObj instanceof Timestamp) {
java.util.Date date = new java.util.Date(((Timestamp)dateObj).getTime());
return sdf.format(date);
}
return null;
}
public static java.util.Date toDate(String dateStr) {
String pattern = "yyyy-MM-dd";
if (dateStr.indexOf(".") > 0) {
pattern = "yyyy-MM-dd HH:mm:ss.SSS";
} else if(dateStr.indexOf(":") > 0) {
pattern = "yyyy-MM-dd HH:mm:ss";
}
return toDate(dateStr, pattern);
}
public static java.util.Date toDate(String dateStr, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern == null ? defaultDatePattern : pattern, Locale.CHINA);
java.util.Date dateReturn = null;
try {
dateReturn = sdf.parse(dateStr);
} catch (ParseException e) {
System.out.println((new StringBuilder("unknown date format:")).append(dateStr).toString());
}
return dateReturn;
}
public static Calendar toCalendar(String dateStr) {
return toCalendar(dateStr, null);
}
public static Calendar toCalendar(String dateStr, String pattern) {
java.util.Date date = toDate(dateStr, pattern);
Calendar calendarReturn = Calendar.getInstance();
calendarReturn.setTime(date);
return calendarReturn;
}
}