处理request数据,存进map



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;
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值