import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
public class CommonUtil {
/**
*
*
* @param request
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Map getParameterMap(HttpServletRequest request) {
//
Map properties = request.getParameterMap();
//
Map returnMap = new HashMap();
Iterator entries = properties.entrySet().iterator();
Map.Entry entry;
String name = "";
String value = "";
while (entries.hasNext()) {
entry = (Map.Entry) entries.next();
name = (String) entry.getKey();
Object valueObj = entry.getValue();
if(null == valueObj){
value = "";
}else if(valueObj instanceof String[]){
String[] values = (String[])valueObj;
for(int i=0;i<values.length;i++){
value = values[i] + ",";
}
value = value.substring(0, value.length()-1);
}else{
value = valueObj.toString();
}
returnMap.put(name, value);
}
return returnMap;
}
public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
if (map == null)
return null;
Object obj = beanClass.newInstance();
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
int mod = field.getModifiers();
if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
continue;
}
field.setAccessible(true);
field.set(obj, map.get(field.getName()));
}
return obj;
}
public static HashMap<String, Object> objectToMap(Object obj) throws Exception {
if(obj == null){
return null;
}
HashMap<String, Object> map = new HashMap<String, Object>();
Field[] declaredFields = obj.getClass().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
if (field.getType().equals(Timestamp.class)) {
map.put(field.getName(), ((Timestamp)field.get(obj)).getTime());
} else {
map.put(field.getName(), field.get(obj));
}
}
return map;
}
/**
*
*/
public static Date[] getDateBetween(Date dt) {
if (dt == null) {
dt = new Date();
}
Date[] dts = new Date[3];
dts[0] = dt;
Calendar c = Calendar.getInstance();
c.setTime(dt);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
dts[1] = c.getTime();
c.add(Calendar.DAY_OF_MONTH, 1);
dts[2] = c.getTime();
return dts;
}
/**
*
*/
public static Date[] getDateBetweenForTrend(Date dt) {
Date[] dts = new Date[3];
dts[0] = dt;
Calendar c = Calendar.getInstance();
c.setTime(dt);
c.add(Calendar.DAY_OF_MONTH, -1);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
dts[1] = c.getTime();
c.add(Calendar.DAY_OF_MONTH, 2);
dts[2] = c.getTime();
return dts;
}
public static void main(String[] args) {
Date[] dts = getDateBetweenForDays(new Date(), 30);
for (int i = 0; i < dts.length; i++) {
System.out.println(DateUtil.getDateTimeFormat(dts[i]));
}
}
public static Date[] getDateBetweenForDays(Date dt, int days) {
Date[] dts = new Date[3];
dts[0] = dt;
Calendar c = Calendar.getInstance();
c.setTime(dt);
c.add(Calendar.DAY_OF_MONTH, 1);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
dts[2] = c.getTime();
c.add(Calendar.DAY_OF_MONTH, -days);
dts[1] = c.getTime();
return dts;
}
public static long getLongValue(Object obj) {
long result = 0;
if (obj != null) {
result = Long.parseLong(String.valueOf(obj));
}
return result;
}
public static long getLongValue(String str) {
long result = 0;
if (StringUtils.isNotEmpty(str)) {
result = Long.parseLong(str);
}
return result;
}
public static int getIntValue(Object obj) {
int result = 0;
if (obj != null) {
result = Integer.parseInt(String.valueOf(obj));
}
return result;
}
public static int getIntValue(String str) {
int result = 0;
if (StringUtils.isNotEmpty(str)) {
result = Integer.parseInt(str);
}
return result;
}
public static String getStringValue(Object obj) {
String result = "";
if (obj != null) {
result = String.valueOf(obj);
}
return result;
}
}
处理request数据,存进map
最新推荐文章于 2024-07-27 22:52:03 发布