添加常用工具类
parent
8cf31497c1
commit
ef4723ad4b
|
@ -23,7 +23,7 @@
|
||||||
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
|
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
|
||||||
</attributes>
|
</attributes>
|
||||||
</classpathentry>
|
</classpathentry>
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.7.0_60">
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
|
||||||
<attributes>
|
<attributes>
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
</attributes>
|
</attributes>
|
||||||
|
|
7
pom.xml
7
pom.xml
|
@ -81,6 +81,13 @@
|
||||||
<artifactId>commons-codec</artifactId>
|
<artifactId>commons-codec</artifactId>
|
||||||
<version>1.6</version>
|
<version>1.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.sf.json-lib</groupId>
|
||||||
|
<artifactId>json-lib</artifactId>
|
||||||
|
<version>2.4</version>
|
||||||
|
<classifier>jdk15</classifier>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<finalName>useful-code</finalName>
|
<finalName>useful-code</finalName>
|
||||||
|
|
|
@ -3,46 +3,103 @@ package osc.git.eh3.test;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.ParsePosition;
|
import java.text.ParsePosition;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import net.sf.json.JSONArray;
|
||||||
|
import net.sf.json.JSONObject;
|
||||||
|
|
||||||
public class TestCode {
|
public class TestCode {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) throws Exception {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
// String pathName = "/dsp/archer/dddfd/jkjl";
|
// String pathName = "/dsp/archer/dddfd/jkjl";
|
||||||
//
|
//
|
||||||
// String projectName = pathName.substring(0, pathName.indexOf("archer"));
|
// String projectName = pathName.substring(0,
|
||||||
//
|
// pathName.indexOf("archer"));
|
||||||
// System.out.println(projectName);
|
//
|
||||||
|
// System.out.println(projectName);
|
||||||
|
|
||||||
|
// SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
// ParsePosition pos = new ParsePosition(0);
|
||||||
|
// System.out.println(formatter.parse("dsd", pos));
|
||||||
|
|
||||||
|
// System.out.println(parseDate("") - 2232);
|
||||||
|
|
||||||
|
// Map<String, Object> resultMap = new HashMap<String, Object>();
|
||||||
|
// System.out.println((String)resultMap.get("dd"));
|
||||||
|
|
||||||
|
// try {
|
||||||
|
// String str = null;
|
||||||
|
// str.equals("");
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// System.out.println(e.getMessage());
|
||||||
|
// e.printStackTrace();
|
||||||
|
// }
|
||||||
|
// System.out.println("fffff");
|
||||||
|
|
||||||
|
// String[] s = {"111","eee"};
|
||||||
|
// System.out.println(Arrays.toString(s));
|
||||||
|
|
||||||
|
// List<String> list = new ArrayList<String>();
|
||||||
|
// list.add("2");
|
||||||
|
// list.add("3");
|
||||||
|
// list.add("7");
|
||||||
|
// list.add("1");
|
||||||
|
//
|
||||||
|
// System.out.println(list.toString());
|
||||||
|
|
||||||
|
// JSONArray areaTarget = new JSONArray();
|
||||||
|
// areaTarget.add("3");
|
||||||
|
// areaTarget.add("5");
|
||||||
|
// areaTarget.add("4");
|
||||||
|
// areaTarget.add("7");
|
||||||
|
// System.out.println(JSONArray.toList(areaTarget));
|
||||||
|
|
||||||
|
|
||||||
// SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
// String whiteStr = "2,4,5,8,3";
|
||||||
// ParsePosition pos = new ParsePosition(0);
|
// System.out.println(JSONArray.fromObject(whiteStr.split(",")));
|
||||||
// System.out.println(formatter.parse("dsd", pos));
|
|
||||||
|
|
||||||
// System.out.println(parseDate("") - 2232);
|
// for (int i = 0;i<2;i++) {
|
||||||
|
//
|
||||||
|
// if ("1".equals("1")) {
|
||||||
|
// if ("1".equals("1")) {
|
||||||
|
// System.out.println("111111111111111");
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
// System.out.println("2222222222222222");
|
||||||
|
// }
|
||||||
|
// System.out.println("3333333333333333333333");
|
||||||
|
// }
|
||||||
|
|
||||||
|
// String str = "http://www.test.com";
|
||||||
|
// System.out.println(str.replace("http://www.", "").replace("www.", ""));
|
||||||
|
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("HH");
|
||||||
|
String str = "23:59:59";
|
||||||
|
System.out.println(sdf.format(formatter.parse(str)));
|
||||||
|
|
||||||
Map<String, Object> resultMap = new HashMap<String, Object>();
|
|
||||||
System.out.println((String)resultMap.get("dd"));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Long parseDate(String s) {
|
public static Long parseDate(String s) {
|
||||||
Long time=null;
|
Long time = null;
|
||||||
if(s==null||""==s){
|
if (s == null || "" == s) {
|
||||||
time = null;
|
time = null;
|
||||||
}else{
|
} else {
|
||||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
try {
|
try {
|
||||||
Date date = format.parse(s);
|
Date date = format.parse(s);
|
||||||
time = date.getTime();
|
time = date.getTime();
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return time;
|
return time;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,102 @@
|
||||||
|
package osc.git.eh3.utils;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class CommonUtils {
|
||||||
|
/**
|
||||||
|
* 对象转换成另一个类对象
|
||||||
|
*
|
||||||
|
* @param bean
|
||||||
|
* 转换的数据对象
|
||||||
|
* @param clazz
|
||||||
|
* 转换后类对象
|
||||||
|
* @return 转换后数据对象
|
||||||
|
*/
|
||||||
|
public static <T> T convertClass(Object bean, Class<T> clazz) {
|
||||||
|
Map<String, Object> maps = new HashMap<String, Object>();
|
||||||
|
T dataBean = null;
|
||||||
|
if (null == bean) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Class<?> cls = bean.getClass();
|
||||||
|
dataBean = clazz.newInstance();
|
||||||
|
Field[] fields = cls.getDeclaredFields();
|
||||||
|
Field[] beanFields = clazz.getDeclaredFields();
|
||||||
|
for (Field field : fields) {
|
||||||
|
try {
|
||||||
|
String fieldName = field.getName();
|
||||||
|
String strGet = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length());
|
||||||
|
Method methodGet = cls.getDeclaredMethod(strGet);
|
||||||
|
Object object = methodGet.invoke(bean);
|
||||||
|
maps.put(fieldName, object == null ? "" : object);
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (Field field : beanFields) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
String fieldName = field.getName();
|
||||||
|
Class<?> fieldType = field.getType();
|
||||||
|
String fieldValue = (maps.get(fieldName) == null || "".equals(maps.get(fieldName))) ? null : maps.get(fieldName).toString();
|
||||||
|
if (fieldValue != null) {
|
||||||
|
if (String.class.equals(fieldType)) {
|
||||||
|
field.set(dataBean, fieldValue);
|
||||||
|
} else if (byte.class.equals(fieldType)) {
|
||||||
|
field.setByte(dataBean, Byte.parseByte(fieldValue));
|
||||||
|
|
||||||
|
} else if (Byte.class.equals(fieldType)) {
|
||||||
|
field.set(dataBean, Byte.valueOf(fieldValue));
|
||||||
|
|
||||||
|
} else if (boolean.class.equals(fieldType)) {
|
||||||
|
field.setBoolean(dataBean, Boolean.parseBoolean(fieldValue));
|
||||||
|
|
||||||
|
} else if (Boolean.class.equals(fieldType)) {
|
||||||
|
field.set(dataBean, Boolean.valueOf(fieldValue));
|
||||||
|
|
||||||
|
} else if (short.class.equals(fieldType)) {
|
||||||
|
field.setShort(dataBean, Short.parseShort(fieldValue));
|
||||||
|
|
||||||
|
} else if (Short.class.equals(fieldType)) {
|
||||||
|
field.set(dataBean, Short.valueOf(fieldValue));
|
||||||
|
|
||||||
|
} else if (int.class.equals(fieldType)) {
|
||||||
|
field.setInt(dataBean, Integer.parseInt(fieldValue));
|
||||||
|
|
||||||
|
} else if (Integer.class.equals(fieldType)) {
|
||||||
|
field.set(dataBean, Integer.valueOf(fieldValue));
|
||||||
|
|
||||||
|
} else if (long.class.equals(fieldType)) {
|
||||||
|
field.setLong(dataBean, Long.parseLong(fieldValue));
|
||||||
|
|
||||||
|
} else if (Long.class.equals(fieldType)) {
|
||||||
|
field.set(dataBean, Long.valueOf(fieldValue));
|
||||||
|
|
||||||
|
} else if (float.class.equals(fieldType)) {
|
||||||
|
field.setFloat(dataBean, Float.parseFloat(fieldValue));
|
||||||
|
|
||||||
|
} else if (Float.class.equals(fieldType)) {
|
||||||
|
field.set(dataBean, Float.valueOf(fieldValue));
|
||||||
|
|
||||||
|
} else if (double.class.equals(fieldType)) {
|
||||||
|
field.setDouble(dataBean, Double.parseDouble(fieldValue));
|
||||||
|
|
||||||
|
} else if (Double.class.equals(fieldType)) {
|
||||||
|
field.set(dataBean, Double.valueOf(fieldValue));
|
||||||
|
|
||||||
|
} else if (Date.class.equals(fieldType)) {
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
||||||
|
field.set(dataBean, sdf.parse(fieldValue));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return dataBean;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue