java 根据省份证号-判断省份-性别-生日

}

public int getDay() {

return day;

}

public void setDay(int day) {

this.day = day;

}

public String getGender() {

return gender;

}

public void setGender(String gender) {

this.gender = gender;

}

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

public IdcardValidator() {

}

public String toString() {

return “省份:” + this.province + “-性别:” + this.gender + “,出生日期:” + new SimpleDateFormat(“yyyy-MM-dd”).format(this.birthday);

}

private Map<String, String> cityCodeMap = new HashMap<String, String>() {

{

this.put(“11”, “北京”);

this.put(“12”, “天津”);

this.put(“13”, “河北”);

this.put(“14”, “山西”);

this.put(“15”, “内蒙古”);

this.put(“21”, “辽宁”);

this.put(“22”, “吉林”);

this.put(“23”, “黑龙江”);

this.put(“31”, “上海”);

this.put(“32”, “江苏”);

this.put(“33”, “浙江”);

this.put(“34”, “安徽”);

this.put(“35”, “福建”);

this.put(“36”, “江西”);

this.put(“37”, “山东”);

this.put(“41”, “河南”);

this.put(“42”, “湖北”);

this.put(“43”, “湖南”);

this.put(“44”, “广东”);

this.put(“45”, “广西”);

this.put(“46”, “海南”);

this.put(“50”, “重庆”);

this.put(“51”, “四川”);

this.put(“52”, “贵州”);

this.put(“53”, “云南”);

this.put(“54”, “西藏”);

this.put(“61”, “陕西”);

this.put(“62”, “甘肃”);

this.put(“63”, “青海”);

this.put(“64”, “宁夏”);

this.put(“65”, “新疆”);

this.put(“71”, “台湾”);

this.put(“81”, “香港”);

this.put(“82”, “澳门”);

this.put(“91”, “国外”);

}

};

private IdcardValidator validator = null;

/**

  • 077

  • 通过构造方法初始化各个成员属性

  • 078

*/

public IdcardValidator(String idcard) {

try {

validator = new IdcardValidator();

if (validator.isValidatedAllIdcard(idcard)) {

if (idcard.length() == 15) {

idcard = validator.convertIdcarBy15bit(idcard);

}

// 获取省份

String provinceId = idcard.substring(0, 2);

Set key = this.cityCodeMap.keySet();

for (String id : key) {

if (id.equals(provinceId)) {

this.province = this.cityCodeMap.get(id);

break;

}

}

// 获取性别

String id17 = idcard.substring(16, 17);

if (Integer.parseInt(id17) % 2 != 0) {

this.gender = “男”;

} else {

this.gender = “女”;

}

// 获取出生日期

String birthday = idcard.substring(6, 14);

Date birthdate = new SimpleDateFormat(“yyyyMMdd”)

.parse(birthday);

this.birthday = birthdate;

GregorianCalendar currentDay = new GregorianCalendar();

currentDay.setTime(birthdate);

this.year = currentDay.get(Calendar.YEAR);

this.month = currentDay.get(Calendar.MONTH) + 1;

this.day = currentDay.get(Calendar.DAY_OF_MONTH);

}

} catch (Exception e) {

  • 19
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值