java 根据省份证号-判断省份-性别-生日,java技术面试问题和答案

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注Java)
img

正文

// 获取前17位

String idcard17 = idcard.substring(0, 17);

// 获取第18位

String idcard18Code = idcard.substring(17, 18);

char c[] = null;

String checkCode = “”;

// 是否都为数字

if (isDigital(idcard17)) {

c = idcard17.toCharArray();

} else {

return false;

}

if (null != c) {

int bit[] = new int[idcard17.length()];

bit = converCharToInt©;

int sum17 = 0;

sum17 = getPowerSum(bit);

// 将和值与11取模得到余数进行校验码判断

checkCode = getCheckCodeBySum(sum17);

if (null == checkCode) {

return false;

}

// 将身份证的第18位与算出来的校码进行匹配,不相等就为假

if (!idcard18Code.equalsIgnoreCase(checkCode)) {

return false;

}

}

return true;

}

/**

  • 159

  • 验证15位身份证的合法性,该方法验证不准确,最好是将15转为18位后再判断,该类中已提供。

  • 160

  • 161

  • @param idcard 162

  • @return 163

*/

public boolean isValidate15Idcard(String idcard) {

// 非15位为假

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

return false;

}

// 是否全都为数字

if (isDigital(idcard)) {

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

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

int year = Integer.parseInt(idcard.substring(6, 8));

int month = Integer.parseInt(idcard.substring(8, 10));

int day = Integer.parseInt(idcard.substring(10, 12));

// 判断是否为合法的省份

boolean flag = false;

for (String id : cityCode) {

if (id.equals(provinceid)) {

flag = true;

break;

}

}

if (!flag) {

return false;

}

// 该身份证生出日期在当前日期之后时为假

Date birthdate = null;

try {

birthdate = new SimpleDateFormat(“yyMMdd”).parse(birthday);

} catch (ParseException e) {

e.printStackTrace();

}

if (birthdate == null || new Date().before(birthdate)) {

return false;

}

// 判断是否为合法的年份

GregorianCalendar curDay = new GregorianCalendar();

int curYear = curDay.get(Calendar.YEAR);

int year2bit = Integer.parseInt(String.valueOf(curYear)

.substring(2));

// 判断该年份的两位表示法,小于50的和大于当前年份的,为假

if ((year < 50 && year > year2bit)) {

return false;

}

// 判断是否为合法的月份

if (month < 1 || month > 12) {

return false;

}

// 判断是否为合法的日期

boolean mflag = false;

curDay.setTime(birthdate); // 将该身份证的出生日期赋于对象curDay

switch (month) {

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

mflag = (day >= 1 && day <= 31);

break;

case 2: // 公历的2月非闰年有28天,闰年的2月是29天。

if (curDay.isLeapYear(curDay.get(Calendar.YEAR))) {

mflag = (day >= 1 && day <= 29);

} else {

mflag = (day >= 1 && day <= 28);

}

break;

case 4:

case 6:

case 9:

case 11:

mflag = (day >= 1 && day <= 30);

break;

}

if (!mflag) {

return false;

}

} else {

return false;

}

return true;

}

/**

  • 253

  • 将15位的身份证转成18位身份证

  • 254

  • 255

  • @param idcard 256

  • @return 257

*/

public String convertIdcarBy15bit(String idcard) {

String idcard17 = null;

// 非15位身份证

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

return null;

}

if (isDigital(idcard)) {

// 获取出生年月日

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

Date birthdate = null;

try {

birthdate = new SimpleDateFormat(“yyMMdd”).parse(birthday);

} catch (ParseException e) {

e.printStackTrace();

}

Calendar cday = Calendar.getInstance();

cday.setTime(birthdate);

String year = String.valueOf(cday.get(Calendar.YEAR));

idcard17 = idcard.substring(0, 6) + year + idcard.substring(8);

char c[] = idcard17.toCharArray();

String checkCode = “”;

if (null != c) {

int bit[] = new int[idcard17.length()];

// 将字符数组转为整型数组

bit = converCharToInt©;

int sum17 = 0;

sum17 = getPowerSum(bit);

// 获取和值与11取模得到余数进行校验码

checkCode = getCheckCodeBySum(sum17);

// 获取不到校验位

if (null == checkCode) {

return null;

}

// 将前17位与第18位校验码拼接

idcard17 += checkCode;

}

} else { // 身份证包含数字

return null;

}

return idcard17;

}

/**

  • 308

  • 15位和18位身份证号码的基本数字和位数验校

  • 309

  • 310

  • @param idcard 311

  • @return 312

*/

public boolean isIdcard(String idcard) {

return idcard == null || “”.equals(idcard) ? false : Pattern.matches(

“(^\d{15} ) ∣ ( d 17 ( ? : d ∣ x ∣ X ) )|(\\d{17}(?:\\d|x|X) )(d17(?:dxX))”, idcard);

}

/**

  • 319

  • 15位身份证号码的基本数字和位数验校

  • 320

  • 321

  • @param idcard 322

  • @return 323

*/

public boolean is15Idcard(String idcard) {

return idcard == null || “”.equals(idcard) ? false : Pattern.matches(

1\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$”,

idcard);

}

/**

  • 331

  • 18位身份证号码的基本数字和位数验校

  • 332

  • 333

  • @param idcard 334

  • @return 335

*/

public boolean is18Idcard(String idcard) {

return Pattern

.matches(

2\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([\d|x|X]{1})$”,

idcard);

}

/**

  • 344

  • 数字验证

  • 345

  • 346

  • @param str 347

  • @return 348

*/

public boolean isDigital(String str) {

return str == null || “”.equals(str) ? false : str.matches(“3*$”);

}

/**

  • 354

  • 将身份证的每位和对应位的加权因子相乘之后,再得到和值

  • 355

  • 356

  • @param bit 357

  • @return 358

*/

public int getPowerSum(int[] bit) {

int sum = 0;

if (power.length != bit.length) {

return sum;

}

for (int i = 0; i < bit.length; i++) {

for (int j = 0; j < power.length; j++) {

if (i == j) {

sum = sum + bit[i] * power[j];

}

}

}

return sum;

}

/**

  • 378

  • 将和值与11取模得到余数进行校验码判断

  • 379

  • 380

  • @param checkCode 381

  • @param sum17 382

  • @return 校验位

  • 383

*/

public String getCheckCodeBySum(int sum17) {

String checkCode = null;

switch (sum17 % 11) {

case 10:

checkCode = “2”;

break;

case 9:

checkCode = “3”;

break;

case 8:

checkCode = “4”;

break;

case 7:

checkCode = “5”;

break;

case 6:

checkCode = “6”;

break;

case 5:

checkCode = “7”;

break;

case 4:

checkCode = “8”;

break;

case 3:

checkCode = “9”;

break;

case 2:

checkCode = “x”;

break;

case 1:

checkCode = “0”;

break;

case 0:

checkCode = “1”;

break;

}

return checkCode;

}

/**

  • 将字符数组转为整型数组

  • @param c

  • @return 429

  • @throws NumberFormatException

*/

public int[] converCharToInt(char[] c) throws NumberFormatException {

int[] a = new int[c.length];

int k = 0;

for (char temp : c) {

a[k++] = Integer.parseInt(String.valueOf(temp));

}

return a;

}

}

测试:

public static void main(String[] args) throws Exception {

String idcard15 = “370283790911703”;//15位

String idcard18 = “37078119790127719x”;//18位

IdcardValidator ie15 = new IdcardValidator(idcard15);

IdcardValidator ie18 = new IdcardValidator(idcard18);

System.out.println(ie15.toString());

System.out.println(ie18.toString());

最后

分布式技术专题+面试解析+相关的手写和学习的笔记pdf

还有更多Java笔记分享如下:

image

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

reak;

case 0:

checkCode = “1”;

break;

}

return checkCode;

}

/**

  • 将字符数组转为整型数组

  • @param c

  • @return 429

  • @throws NumberFormatException

*/

public int[] converCharToInt(char[] c) throws NumberFormatException {

int[] a = new int[c.length];

int k = 0;

for (char temp : c) {

a[k++] = Integer.parseInt(String.valueOf(temp));

}

return a;

}

}

测试:

public static void main(String[] args) throws Exception {

String idcard15 = “370283790911703”;//15位

String idcard18 = “37078119790127719x”;//18位

IdcardValidator ie15 = new IdcardValidator(idcard15);

IdcardValidator ie18 = new IdcardValidator(idcard18);

System.out.println(ie15.toString());

System.out.println(ie18.toString());

最后

分布式技术专题+面试解析+相关的手写和学习的笔记pdf

还有更多Java笔记分享如下:

[外链图片转存中…(img-hI6DLoB3-1713608668090)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
[外链图片转存中…(img-l45hvztG-1713608668091)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!


  1. 1-9 ↩︎

  2. 1-9 ↩︎

  3. 0-9 ↩︎

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值