這兩天做一個(gè)身份證校驗(yàn)的問(wèn)題,碰到了日期校驗(yàn)的問(wèn)題,為了搞的簡(jiǎn)單一點(diǎn),查了很多資料,但都不太理想。后來(lái)想到一個(gè)方法,那就是通過(guò)值比較的方法。比如你要校驗(yàn)的日期為:
它就會(huì)自動(dòng)轉(zhuǎn)換為:
public static java.sql.Date getSystemDate() throws Exception {
SimpleDateFormat tempSimpleDateFormat = null;
Date currentDate = null;
try{
tempSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
currentDate = java.sql.Date.valueOf(tempSimpleDateFormat.format(new java.
util.Date()));
}catch(Exception ex){
ex.printStackTrace();
throw ex;
}finally{
tempSimpleDateFormat = null;
}
return currentDate;
}。
下面是一個(gè)身份證校驗(yàn)的方法(說(shuō)明:除了日期校驗(yàn)和數(shù)字校驗(yàn)外,其他校驗(yàn)方法是從互聯(lián)網(wǎng)上搜索得到的,但卻沒(méi)有搜到原作者,也沒(méi)有搜到原出處,以后一定補(bǔ)上,特此致歉。)
package com.hyq.test.src;
import java.text.SimpleDateFormat;
import java.sql.Date;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class VerifyIdcard {
public VerifyIdcard() {
}
/**
* VerifyIdCard
*
* @param idcard String
* @return boolean
*/
public boolean VerifyIdCard(String idcard) {
Pattern pattern = null;
Matcher matcher = null;
Date currentDate = null;
Date conversionDateOne = null; //從身份證獲取日期
String year = null; //從身份證獲取年
String month = null; //從身份證獲取月
String day = null; //從身份證獲取日
String conversionDateTwo = null;//將日期轉(zhuǎn)換為字符串(中間無(wú)符號(hào))
String verifyCode = null; //最后一位校驗(yàn)位
if (idcard.length() == 15) {
idcard = uptoeighteen(idcard);
}
if (idcard.length() != 18) {
return false;
}
try{
pattern = Pattern.compile("\\d{17}");
matcher = pattern.matcher(idcard);
if (!matcher.find()) {
return false;
}
year = idcard.substring(6, 10);
month = idcard.substring(10, 12);
day = idcard.substring(12, 14);
long dateOne = Long.parseLong(idcard.substring(6, 14));
currentDate = this.getSystemDate();
conversionDateOne = java.sql.Date.valueOf(year + "-" + month + "-" +
day);
if (currentDate.compareTo(conversionDateOne) <= 0) {
return false;
}
conversionDateTwo = conversionDateOne.toString().substring(0, 4) +
conversionDateOne.toString().substring(5, 7) +
conversionDateOne.toString().substring(8);
long dateTwo = Long.parseLong(conversionDateTwo);
if (dateTwo > dateOne) {
return false;
}
verifyCode = idcard.substring(17);
if(verifyCode != null && verifyCode.equals(this.getVerify(idcard.substring(0,17)))){
return true;
}else{
return false;
}
}catch(Exception ex){
return false;
}finally{
pattern = null;
matcher = null;
currentDate = null;
conversionDateOne = null;
year = null;
month = null;
day = null;
conversionDateTwo = null;
}
}
//get verify
/**
* getVerify
*
* @param eightcardid String
* @return String
*/
public String getVerify(String eightcardid) {
int remaining = 0;
int[] wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
int[] vi = {1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2};
int[] ai = new int[18];
String returnStr = null;
try{
if (eightcardid.length() == 18) {
eightcardid = eightcardid.substring(0, 17);
}
if (eightcardid.length() == 17) {
int sum = 0;
String k = null;
for (int i = 0; i < 17; i++) {
k = eightcardid.substring(i, i + 1);
ai[i] = Integer.parseInt(k);
k = null;
}
for (int i = 0; i < 17; i++) {
sum = sum + wi[i] * ai[i];
}
remaining = sum % 11;
}
returnStr = remaining == 2 ? "X" : String.valueOf(vi[remaining]);
}
catch(Exception ex){
return null;
}finally{
wi = null;
vi = null;
ai = null;
}
return returnStr;
}
//獲取當(dāng)前系統(tǒng)日期
/**
* getSystemDate
*
* @return Date
*/
public static java.sql.Date getSystemDate(){
SimpleDateFormat tempSimpleDateFormat = null;
Date currentDate = null;
try{
tempSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
currentDate = java.sql.Date.valueOf(tempSimpleDateFormat.format(new java.
util.Date()));
}catch(Exception ex){
return null;
}finally{
tempSimpleDateFormat = null;
}
return currentDate;
}
//15 update to 18
/**
* uptoeighteen
*
* @param fifteencardid String
* @return String
*/
public String uptoeighteen(String fifteencardid) {
String eightcardid = fifteencardid.substring(0, 6);
eightcardid = eightcardid + "19";
eightcardid = eightcardid + fifteencardid.substring(6, 15);
eightcardid = eightcardid + getVerify(eightcardid);
return eightcardid;
}
public static void main(String[] args){
String idCard = "410327198107122438";
VerifyIdcard tempVerifyIdcard = new VerifyIdcard();
if(tempVerifyIdcard.VerifyIdCard(idCard)){
System.out.println("+++++++++++++++++++");
}else{
System.out.println("*********************");
}
}
}