当前位置:首页 > 企业简介 >

万能对象拷贝工具 支持不同数据类型

万能对象拷贝工具 支持不同数据类型 [html]

import java.lang.reflect.Field;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

public class BeanUtil {

/**

* 拷贝相同属性

* 从src 复制到 des

*/

public static void copyPropertys(Object src, Object des){

ArrayList<Field> srcFields = new ArrayList<Field>();

HashMap<String,Field> desFields = new HashMap<String,Field>();

for(Field field : src.getClass().getDeclaredFields()){

srcFields.add(field);

}

for(Field field : des.getClass().getDeclaredFields()){

desFields.put(field.getName(),field);

}

for(Field srcfield :srcFields){

try {

if(desFields.containsKey(srcfield.getName()))

{

Field desField = desFields.get(srcfield.getName());

if(srcfield.getName().equals("serialVersionUID")){

continue;

}

desField.setAccessible(true);

srcfield.setAccessible(true);

if(srcfield.getType()!=desField.getType())

{

if((srcfield.getType() == java.util.Date.class || srcfield.getType() == java.sql.Date.class || srcfield.getType() ==

java.sql.Timestamp.class) && desField.getType() == java.lang.String.class)

{

java.util.Date dates = (Date) srcfield.get(src);

desField.set(des, formatDate(dates));

}else

{

if(desField.getType() == java.util.Date.class || desField.getType() == java.sql.Date.class || desField.getType() == java.sql.Timestamp.class )

{

java.util.Date date = ToDate(String.valueOf(srcfield.get(src)));

desField.set(des,date);

}else if(desField.getType() == java.lang.Integer.class || desField.getType() == int.class )

{

Integer ints =java.lang.Integer.valueOf(String.valueOf(srcfield.get(src)));

desField.set(des,ints);

}else if(desField.getType() == java.lang.Long.class )

{

java.lang.Long longs = java.lang.Long.getLong(String.valueOf(srcfield.get(src)));

desField.set(des,longs);

}else{

desField.set(des,String.valueOf(srcfield.get(src)));

}

}

}else

{

desField.set(des,srcfield.get(src));

}

desField.setAccessible(false);

srcfield.setAccessible(false);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

/**

* 把Date格式成yyyy-MM-dd HH:mm:ss格式的字符串

*/

public static String formatDate(Date date) {

if (date == null)

return "";

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

return df.format(date);

}

/**

* 把Date格式成yyyy-MM-dd HH:mm:ss格式的字符串

*/

public static Date ToDate(String date) throws ParseException {

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

return df.parse(date);

}

}

import java.lang.reflect.Field;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

public class BeanUtil {

/**

* 拷贝相同属性

* 从src 复制到 des

*/

public static void copyPropertys(Object src, Object des){

ArrayList<Field> srcFields = new ArrayList<Field>();

HashMap<String,Field> desFields = new HashMap<String,Field>();

for(Field field : src.getClass().getDeclaredFields()){

srcFields.add(field);

}

for(Field field : des.getClass().getDeclaredFields()){

desFields.put(field.getName(),field);

}

for(Field srcfield :srcFields){

try {

if(desFields.containsKey(srcfield.getName()))

{

Field desField = desFields.get(srcfield.getName());

if(srcfield.getName().equals("serialVersionUID")){

continue;

}

desField.setAccessible(true);

srcfield.setAccessible(true);

if(srcfield.getType()!=desField.getType())

{

if((srcfield.getType() == java.util.Date.class || srcfield.getType() == java.sql.Date.class || srcfield.getType() ==

java.sql.Timestamp.class) && desField.getType() == java.lang.String.class)

{

java.util.Date dates = (Date) srcfield.get(src);

desField.set(des, formatDate(dates));

}else

{

if(desField.getType() == java.util.Date.class || desField.getType() == java.sql.Date.class || desField.getType() == java.sql.Timestamp.class )

{

java.util.Date date = ToDate(String.valueOf(srcfield.get(src)));

desField.set(des,date);

}else if(desField.getType() == java.lang.Integer.class || desField.getType() == int.class )

{

Integer ints =java.lang.Integer.valueOf(String.valueOf(srcfield.get(src)));

desField.set(des,ints);

}else if(desField.getType() == java.lang.Long.class )

{

java.lang.Long longs = java.lang.Long.getLong(String.valueOf(srcfield.get(src)));

desField.set(des,longs);

}else{

desField.set(des,String.valueOf(srcfield.get(src)));

}

}

}else

{

desField.set(des,srcfield.get(src));

}

desField.setAccessible(false);

srcfield.setAccessible(false);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

/**

* 把Date格式成yyyy-MM-dd HH:mm:ss格式的字符串

*/

public static String formatDate(Date date) {

if (date == null)

return "";

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

return df.format(date);www.2cto.com

}

/**

* 把Date格式成yyyy-MM-dd HH:mm:ss格式的字符串

*/

public static Date ToDate(String date) throws ParseException {

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

return df.parse(date);

}

}

,采集软件,站群

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网站设计 https://www.feimao666.com

上一篇:JSP简明教程

下一篇:最后一页