您的位置:首页 > 其它

随机生成身份证号码

2014-09-10 15:22 507 查看
   //程序入口
   public static void main ( String[] args )
   {
Test t = new Test();
for (int i = 0; i < 100; i++)//随机生成100个身份证号码
{
System.out.println(t.getRandomID();
     }
   }


/**
* 获取随机生成的身份证号码
*
* @author mingzijian
* @return
*/
public String getRandomID() {
String id = "420222199204179999";
// 随机生成省、自治区、直辖市代码 1-2
String provinces[] = { "11", "12", "13", "14", "15", "21", "22", "23",
"31", "32", "33", "34", "35", "36", "37", "41", "42", "43",
"44", "45", "46", "50", "51", "52", "53", "54", "61", "62",
"63", "64", "65", "71", "81", "82" };
String province = randomOne(provinces);
// 随机生成地级市、盟、自治州代码 3-4
String city = randomCityCode(18);
// 随机生成县、县级市、区代码 5-6
String county = randomCityCode(28);
// 随机生成出生年月日 7-14
String birth = randomBirth(20, 40);
// 随机生成顺序码 15-17 对同区域同生日的人员进行编序,其中第17位(女:偶数  男:奇数)
Random rd=new Random();
String no = rd.nextInt(10)+""+rd.nextInt(10)+""+rd.nextInt(10);
// 随机生成校验码 18
String checks[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9","X" };
String check = randomOne(checks);
// 拼接身份证号码
id = province + city + county + birth + no + check;
return id;
}

/**
* 从String[] 数组中随机取出其中一个String字符串
*  new Random().nextInt(10)————>>区间是:[0,10),包含0但不包含10
* @author mingzijian
* @param s
* @return
*/
public String randomOne(String s[]) {
return s[new Random().nextInt(s.length)];
}

/**
* 随机生成两位数的字符串(01-max),不足两位的前面补0
*
* @author mingzijian
* @param max
* @return
*/
public String randomCityCode(int max) {
int i = new Random().nextInt(max) + 1;
return i > 9 ? i + "" : "0" + i;
}

/**
* 随机生成minAge到maxAge年龄段的人的生日日期
*
* @author mingzijian
* @param minAge
* @param maxAge
* @return
*/
public String randomBirth(int minAge, int maxAge) {
SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");// 设置日期格式
Calendar date = Calendar.getInstance();
date.setTime(new Date());// 设置当前日期
// 随机设置日期为前maxAge年至前minAge年的任意一天
int randomDay = 365 * minAge  + new Random().nextInt(365 * (maxAge - minAge));
date.set(Calendar.DATE, date.get(Calendar.DATE) - randomDay);
return dft.format(date.getTime());
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: