자바 주민등록번호 검사
6277 단어 Java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main1 {
/**
* 15
*/
private static final Integer FIFTEEN_ID_CARD = 15;
/**
* 18
*/
private static final Integer EIGHTEEN_ID_CARD = 18;
private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
/**
*
* @param IDCard
* @return
*/
public static String getSex(String IDCard) {
String sex = "";
if (IDCard != null) {
//15
if (IDCard.length() == FIFTEEN_ID_CARD) {
if (Integer.parseInt(IDCard.substring(14, 15)) % 2 == 0) {
sex = " ";
} else {
sex = " ";
}
//18
} else if (IDCard.length() == EIGHTEEN_ID_CARD) {
//
if (Integer.parseInt(IDCard.substring(16).substring(0, 1))
% 2 == 0) {
sex = " ";
} else {
sex = " ";
}
}
}
return sex;
}
/**
*
* @param IDCard
* @return
*/
public static Integer getAge(String IDCard) {
Integer age = 0;
Date date = new Date();
if (IDCard != null && isValid(IDCard)) {
//15
if (IDCard.length() == FIFTEEN_ID_CARD) {
// (15 1980 )
String uyear = "19" + IDCard.substring(6, 8);
//
String uyue = IDCard.substring(8, 10);
//
String fyear = format.format(date).substring(0, 4);
//
String fyue = format.format(date).substring(5, 7);
if (Integer.parseInt(uyue) <= Integer.parseInt(fyue)) {
age = Integer.parseInt(fyear) - Integer.parseInt(uyear) + 1;
//
} else {
age = Integer.parseInt(fyear) - Integer.parseInt(uyear);
}
//18
} else if (IDCard.length() == EIGHTEEN_ID_CARD) {
//
String year = IDCard.substring(6).substring(0, 4);
//
String yue = IDCard.substring(10).substring(0, 2);
//
String fyear = format.format(date).substring(0, 4);
//
String fyue = format.format(date).substring(5, 7);
//
if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) {
age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
//
} else {
age = Integer.parseInt(fyear) - Integer.parseInt(year);
}
}
}
return age;
}
/**
* yyyy MM dd
* @param IDCard
* @return
*/
public static String getBirthday(String IDCard) {
String birthday = "";
String year = "";
String month = "";
String day = "";
if (IDCard != null) {
//15
if (IDCard.length() == FIFTEEN_ID_CARD) {
// (15 1980 )
year = "19" + IDCard.substring(6, 8);
//
month = IDCard.substring(8, 10);
//
day = IDCard.substring(10, 12);
//18
} else if (IDCard.length() == EIGHTEEN_ID_CARD) {
//
year = IDCard.substring(6).substring(0, 4);
//
month = IDCard.substring(10).substring(0, 2);
//
day = IDCard.substring(12).substring(0, 2);
}
birthday = year + " " + month + " " + day + " ";
}
return birthday;
}
/**
*
* @param id
* @return
*/
public static boolean isValid(String id) {
Boolean validResult = true;
// 15 18
int len = id.length();
if (len != FIFTEEN_ID_CARD && len != EIGHTEEN_ID_CARD) {
validResult = false;
}
//
if (!validDate(id)) {
validResult = false;
}
return validResult;
}
/**
*
* @param id
* @return
*/
private static boolean validDate(String id) {
try {
String birth = id.length() == 15 ? "19" + id.substring(6, 12)
: id.substring(6, 14);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date birthDate = sdf.parse(birth);
if (!birth.equals(sdf.format(birthDate))) {
return false;
}
} catch (ParseException e) {
return false;
}
return true;
}
/**
* 18
* @param cardId
* @return true
*/
public static boolean id18check(String cardId) {
if (StringUtils.isNotBlank(cardId)) {
char[] id = cardId.toCharArray();
int sum = 0;
int w[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
char[] ch = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3',
'2' };
for (int i = 0; i < id.length - 1; i++) {
sum += (id[i] - '0') * w[i];
}
int c = sum % 11;
char code = ch[c];
char last = id[id.length - 1];
last = last == 'x' ? 'X' : last;
return last == code;
}
return false;
}
public static void main(String[] args) {
boolean a = validDate("218591199713191221");
System.out.println(a);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.