工具类


import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class IdCardGenerator {

    static final Map<String, Integer> areaCode = new HashMap<String, Integer>();
    static {
        IdCardGenerator.areaCode.put("北京市", 110000);
        IdCardGenerator.areaCode.put("市辖区", 110100);
        IdCardGenerator.areaCode.put("东城区", 110101);
        IdCardGenerator.areaCode.put("西城区", 110102);
        IdCardGenerator.areaCode.put("崇文区", 110103);
        IdCardGenerator.areaCode.put("宣武区", 110104);
        IdCardGenerator.areaCode.put("朝阳区", 110105);
        IdCardGenerator.areaCode.put("丰台区", 110106);
        IdCardGenerator.areaCode.put("石景山区", 110107);
        IdCardGenerator.areaCode.put("海淀区", 110108);
        IdCardGenerator.areaCode.put("门头沟区", 110109);
        IdCardGenerator.areaCode.put("房山区", 110111);
        IdCardGenerator.areaCode.put("通州区", 110112);
        IdCardGenerator.areaCode.put("顺义区", 110113);
        IdCardGenerator.areaCode.put("昌平区", 110114);
        IdCardGenerator.areaCode.put("大兴区", 110115);
        IdCardGenerator.areaCode.put("怀柔区", 110116);
        IdCardGenerator.areaCode.put("平谷区", 110117);
        IdCardGenerator.areaCode.put("上海市", 310000);
        IdCardGenerator.areaCode.put("市辖区", 310100);
        IdCardGenerator.areaCode.put("黄浦区", 310101);
        IdCardGenerator.areaCode.put("卢湾区", 310103);
        IdCardGenerator.areaCode.put("徐汇区", 310104);
        IdCardGenerator.areaCode.put("长宁区", 310105);
        IdCardGenerator.areaCode.put("静安区", 310106);
        IdCardGenerator.areaCode.put("普陀区", 310107);
        IdCardGenerator.areaCode.put("闸北区", 310108);
        IdCardGenerator.areaCode.put("虹口区", 310109);
        IdCardGenerator.areaCode.put("杨浦区", 310110);
        IdCardGenerator.areaCode.put("闵行区", 310112);
        IdCardGenerator.areaCode.put("宝山区", 310113);
        IdCardGenerator.areaCode.put("嘉定区", 310114);
        IdCardGenerator.areaCode.put("浦东新区 (*)", 310115);
        IdCardGenerator.areaCode.put("金山区", 310116);
        IdCardGenerator.areaCode.put("松江区", 310117);
        IdCardGenerator.areaCode.put("青浦区", 310118);
        IdCardGenerator.areaCode.put("奉贤区", 310120);
        IdCardGenerator.areaCode.put("江苏省", 320000);
        IdCardGenerator.areaCode.put("南京市", 320100);
        IdCardGenerator.areaCode.put("市辖区", 320101);
        IdCardGenerator.areaCode.put("玄武区", 320102);
        IdCardGenerator.areaCode.put("白下区", 320103);
        IdCardGenerator.areaCode.put("秦淮区", 320104);
        IdCardGenerator.areaCode.put("建邺区", 320105);
        IdCardGenerator.areaCode.put("鼓楼区", 320106);
        IdCardGenerator.areaCode.put("下关区", 320107);
        IdCardGenerator.areaCode.put("浦口区", 320111);
        IdCardGenerator.areaCode.put("栖霞区", 320113);
        IdCardGenerator.areaCode.put("雨花台区", 320114);
        IdCardGenerator.areaCode.put("江宁区", 320115);
        IdCardGenerator.areaCode.put("六合区", 320116);
        IdCardGenerator.areaCode.put("苏州市", 320500);
        IdCardGenerator.areaCode.put("市辖区", 320501);
        IdCardGenerator.areaCode.put("沧浪区", 320502);
        IdCardGenerator.areaCode.put("平江区", 320503);
        IdCardGenerator.areaCode.put("金阊区", 320504);
        IdCardGenerator.areaCode.put("虎丘区", 320505);
        IdCardGenerator.areaCode.put("吴中区", 320506);
        IdCardGenerator.areaCode.put("相城区", 320507);
        IdCardGenerator.areaCode.put("常熟市", 320581);
        IdCardGenerator.areaCode.put("张家港市", 320582);
        IdCardGenerator.areaCode.put("昆山市", 320583);
        IdCardGenerator.areaCode.put("吴江市", 320584);
        IdCardGenerator.areaCode.put("太仓市", 320585);
        IdCardGenerator.areaCode.put("南通市", 320600);
        IdCardGenerator.areaCode.put("河南省", 410000);
        IdCardGenerator.areaCode.put("郑州市", 410100);
        IdCardGenerator.areaCode.put("市辖区", 410101);
        IdCardGenerator.areaCode.put("中原区", 410102);
        IdCardGenerator.areaCode.put("二七区", 410103);
        IdCardGenerator.areaCode.put("管城回族区", 410104);
        IdCardGenerator.areaCode.put("金水区", 410105);
        IdCardGenerator.areaCode.put("上街区", 410106);
        IdCardGenerator.areaCode.put("惠济区", 410108);
    }

    public static int randomAreaCode() {
        int index = (int) (Math.random() * IdCardGenerator.areaCode.size());
        Collection<Integer> values = IdCardGenerator.areaCode.values();
        Iterator<Integer> it = values.iterator();
        int i = 0;
        int code = 0;
        while (i < index && it.hasNext()) {
            i++;
            code = it.next();
        }
        return code;
    }

    public static String randomBirthday() {
        Calendar birthday = Calendar.getInstance();
        birthday.set(Calendar.YEAR, (int) (Math.random() * 60) + 1950);
        birthday.set(Calendar.MONTH, (int) (Math.random() * 12));
        birthday.set(Calendar.DATE, (int) (Math.random() * 31));

        StringBuilder builder = new StringBuilder();
        builder.append(birthday.get(Calendar.YEAR));
        long month = birthday.get(Calendar.MONTH) + 1;
        if (month < 10) {
            builder.append("0");
        }
        builder.append(month);
        long date = birthday.get(Calendar.DATE);
        if (date < 10) {
            builder.append("0");
        }
        builder.append(date);
        return builder.toString();
    }

    public static char calcTrailingNumber(char[] chars) {
        if (chars.length < 17) {
            return ' ';
        }
        int[] c = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
        char[] r = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
        int[] n = new int[17];
        int result = 0;
        for (int i = 0; i < n.length; i++) {
            n[i] = Integer.parseInt(chars[i] + "");
        }
        for (int i = 0; i < n.length; i++) {
            result += c[i] * n[i];
        }
        return r[result % 11];
    }

    public static String randomCode() {
        int code = (int) (Math.random() * 1000);
        if (code < 10) {
            return "00" + code;
        } else if (code < 100) {
            return "0" + code;
        } else {
            return "" + code;
        }
    }

    public static String generate() {
        StringBuilder generater = new StringBuilder();
        generater.append(randomAreaCode());
        generater.append(randomBirthday());
        generater.append(randomCode());
        generater
                .append(calcTrailingNumber(generater.toString().toCharArray()));
        return generater.toString();
    }

        String IdCard = generate();
                vars.put("IdCard ",IdCard);
                 return IdCard;
}

 

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import net.minidev.json.JSONArray;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Predicate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Utils {

    /**
     * 读取文本文件
     * 
     * @param filePath
     * @return
     */
    public static String readJson(String filePath) {
        String charset = "utf-8";
        File file = new File(filePath);
        long fileByteLength = file.length();
        byte[] content = new byte[(int) fileByteLength];
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String str = null;
        try {
            str = new String(content, charset);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 提取Json 的Value值
     * 
     * @param json
     * @param jsonPath
     * @return
     */
    public static String readjson(String json, String jsonPath) {
        try {
            Object value = JsonPath.read(json, jsonPath, new Predicate[0]);
            if (value instanceof Integer) {
                return value.toString();
            } else if (value instanceof String) {
                return value.toString();
            } else if (value instanceof Boolean) {
                return value.toString();
            } else if (value instanceof JSONArray) {
                JSONArray arr = (JSONArray) value;
                if (!arr.isEmpty()) {
                    return arr.toJSONString();
                } else
                    return "";
            } else if (value instanceof LinkedHashMap) {
                return value.toString();
            } else if (value instanceof Float) {
                return value.toString();
            } else {
                return value.toString();
            }
        } catch (Exception e) {
            return "pathnotfound";
        }
    }

    /**
     * 判断一个文件是否存在
     * 
     * @param file
     * @param filePath
     * @return
     */
    public static boolean FileExists(File file, String filePath) {
        file = new File(filePath);
        if (file.exists()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取当前日期
     * 
     * @return
     */
    public String getcurrentDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate = formatter.format(currentTime);
        return currentDate;
    }

    /**
     * 获取当前日期的下一个月
     * 
     * @param day
     * @return
     */
    public String getDateAddMonth() {
        String year_month_day = "yyyy-MM-dd";
        SimpleDateFormat dgt = new SimpleDateFormat(year_month_day);
        Calendar date = Calendar.getInstance();
        date.add(Calendar.MONTH, Integer.parseInt("1"));
        String endDate = dgt.format(date.getTime());
        return endDate;
    }

    /**
     * 获取当前日期减一天
     * 
     * @return
     */
    public String getDateBeforeDay() {
        Calendar c = Calendar.getInstance();
        Date date = null;
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String a = formatter.format(currentTime);
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(a);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - 1);
        String dayBefroe = new SimpleDateFormat("yyyy-MM-dd").format(c
                .getTime());

        return dayBefroe;
    }

}

 

//获取get请求地址参数的对应的值
import java.util.Map;
import com.google.common.base.Splitter;

public class Test {

 public static void main(String[] args) {
  System.out.println(getParam("generalActivityNew/couponList?id=269&level=1&userTag=0","level"));
 }

 public static String getParam(String url, String name) {
  String params = url.substring(url.indexOf("?") + 1, url.length());
  Map<String, String> split = Splitter.on("&").withKeyValueSeparator("=").split(params);
  return split.get(name);
 }

 

     //提取字符串中的数字
 
    public static Long getNumber(String str){
 
        Pattern p = Pattern.compile("([0-9]+)");
 
        Matcher m = p.matcher(str);
 
        while(m.find()){
 
            return Long.parseLong(m.group(1));
 
        }
 
        return null;
 
    }

 

posted @ 2017-07-25 14:15  vame  Views(226)  Comments(0Edit  收藏  举报