快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

澳门威泥斯人:中华人民共和国公民身份证号码校验程序



/**

* roonten.practice.tools.identitycard\IdentityCard.java

*

* Copyright (c) 2008 patulous function toolkit for java, individual

* All rights reserved.

*

* This toolkit all my items one is the last edition. All the tools in the toolkit are defining

* some handy discreteness for me. So I can doing items easily. I hope you unfeignedly

* for use it.

*/

package roonten.practice.tools.identitycard;

import java.util.Calendar;

/**

*

* The IdentityCard class is implements IIdentityCard

* interface.In this interface there're some about checking an identity

* card need to using methods. For example, the year and the month and the day

* part this identity card must be right.

*

*

* @author Roon Ten

* @version 1.2

*/

public class IdentityCard {

private char[] chrs;

private final String CHECK_STRING0 = "([0-9]{17})[0-9Xx]";

/**

* Create an object of this class by an scheduled identity card.

* @param obj

*/

public IdentityCard(Object obj) {

chrs = getChrs(obj);

}

/**

* Get a team for character by this identity card.

* @param obj

* @return

*/

private char[] getChrs(Object obj) {

return String.valueOf(obj).toCharArray();

}

/**

* If this identity card is legal one, return true.

*/

public boolean isLegalityFormat() {

return String.valueOf(this.chrs).matches(CHECK_STRING0);

}

/**

* If t澳门威泥斯人he year this identity card is legal one, return true.

*/

public boolean isLegalityYear() {

boolean澳门威泥斯人 isPass = false;

if (chrs != null && chrs.length >= 18) {

int澳门威泥斯人 year = Integer.parseInt(chrs[6] + "" + chrs[7] + "" + chrs[8]

+ "" + chrs澳门威泥斯人[9]);

int current = Calendar.getInstance().get(Calendar.YEAR);

isPass = year >= current - 100 && year = 18) {

int month = Integer.parseInt(chrs[10] + "" + chrs[11]);

isPass = month > 0 && month = 18) {

int yy = Integer.parseInt(chrs[6] + "" + chrs[7] + "" + chrs[8]

+ "" + chrs[9]);

int month = Integer.parseInt(chrs[10] + "" + chrs[11]);

int day = Integer.parseInt(chrs[12] + "" + chrs[13]);

Year year = new Year(yy);

isPass = year.getMaxDay(month) >= day;

}

return isPass;

}

/**

* If the last character this identity card is legal one, return true.

*/

public boolean isLegalityEnd() {

boolean isPass = false;

if (chrs != null && chrs.length >= 18) {

int num = (Integer.parseInt(chrs[0] + "") * 7

+ Integer.parseInt(chrs[1] + "") * 9

+ Integer.parseInt(chrs[2] + "") * 10

+ Integer.parseInt(chrs[3] + "") * 5

+ Integer.parseInt(chrs[4] + "") * 8

+ Integer.parseInt(chrs[5] + "") * 4

+ Integer.parseInt(chrs[6] + "") * 2

+ Integer.parseInt(chrs[7] + "") * 1

+ Integer.parseInt(chrs[8] + "") * 6

+ Integer.parseInt(chrs[9] + "") * 3

+ Integer.parseInt(chrs[10] + "") * 7

+ Integer.parseInt(chrs[11] + "") * 9

+ Integer.parseInt(chrs[12] + "") * 10

+ Integer.parseInt(chrs[13] + "") * 5

+ Integer.parseInt(chrs[14] + "") * 8

+ Integer.parseInt(chrs[15] + "") * 4 + Integer

.parseInt(chrs[16] + "") * 2) % 11;

switch (num) {

case 0:

isPass = '1' == chrs[17];

break;

case 1:

isPass = '0' == chrs[17];

break;

case 2:

isPass = 'X' == chrs[17] || 'x' == chrs[17];

break;

case 3:

isPass = '9' == chrs[17];

break;

case 4:

isPass = '8' == chrs[17];

break;

case 5:

isPass = '7' == chrs[17];

break;

case 6:

isPass = '6' == chrs[17];

break;

case 7:

isPass = '5' == chrs[17];

break;

case 8:

isPass = '4' == chrs[17];

break;

case 9:

isPass = '3' == chrs[17];

break;

case 10:

isPass = '2' == chrs[17];

break;

}

}

return isPass;

}

/**

* Get the length this identity card.

*/

public int getLength() {

return this.chrs.length;

}

/**

* If it is a legal one.

*/

public boolean isEffective() {

boolean isValid = false;

isValid = isLegalityFormat();

if (isValid) {

isValid = isLegalityYear();

if (isValid) {

isValid = isLegalityMonth();

if (isValid) {

isValid = isLegalityDay();

if (isValid) {

isValid = isLegalityEnd();

}

}

}

}

return isValid;

}

}

/**

* The Year class use to get a month day of a year.

* @author TenRoon

* @version 1.2

*/

class Year {

/**

* Current year.

*/

private int Year;

/**

* January days.

*/

public static final int JAN_DAY = 31;

/**

* February days of a year.

* @param year

* @return

*/

public static final int FEB_DAY(int year) {

int feb_Day = 28;

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {

feb_Day = 29;

}

return feb_Day;

}

/**

* March days.

*/

public static final int MAR_DAY = 31;

/**

* April days.

*澳门威泥斯人/

public static final int APR_DAY = 30;

/**

* May days.

*/

public static final int MAY_DAY = 31;

/**

* June days.

*/

public static final int JUN_DAY = 30;

/**

* July days.

*/

public static final int JUL_DAY = 31;

/**

* August days.

*/

public static final int AUG_DAY = 31;

/**

* September days.

*/

public static final int SEP_DAY = 30;

/**

* Octomber days.

*/

public static final int OCT_DAY = 31;

/**

* November days.

*/

public static final int NOV_DAY = 30;

/**

* December days.

*/

public static final int DEC_DAY = 31;

/**

* Create a object of a current year.

* @param Year

*/

public Year(int Year) {

this.Year = Year;

}

/**

* Get the max day a month.

* @param month

* @return

*/

public int getMaxDay(int month) {

int maxDay = 0;

switch (month) {

case 1:

maxDay = JAN_DAY;

break;

case 2:

maxDay = FEB_DAY(this.Year);

break;

case 3:

maxDay = MAR_DAY;

break;

case 4:

maxDay = APR_DAY;

break;

case 5:

maxDay = MAY_DAY;

break;

case 6:

maxDay = JUN_DAY;

break;

case 7:

maxDay = JUL_DAY;

break;

case 8:

maxDay = AUG_DAY;

break;

case 9:

maxDay = SEP_DAY;

break;

case 10:

maxDay = OCT_DAY;

break;

case 11:

maxDay = NOV_DAY;

break;

case 12:

maxDay = DEC_DAY;

break;

}

return maxDay;

}

}

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: