public static void main(String[] args){
你所需要的網(wǎng)站建設服務,我們均能行業(yè)靠前的水平為你提供.標準是產(chǎn)品質量的保證,主要從事網(wǎng)站設計、做網(wǎng)站、企業(yè)網(wǎng)站建設、成都做手機網(wǎng)站、網(wǎng)頁設計、高端網(wǎng)站設計、網(wǎng)頁制作、做網(wǎng)站、建網(wǎng)站。成都創(chuàng)新互聯(lián)擁有實力堅強的技術研發(fā)團隊及素養(yǎng)的視覺設計專才。
Scanner input = new Scanner(System.in);
System.out.println("請輸入十八位身份證號碼!");
String id=input.next();
if(id.length()!=18){
System.out.println("身份證號碼長度不對!");
}
String top=id.substring(0, 17);
String sishu="7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2";
String[] xishu=sishu.split(",");
int sum=0;
for (int i = 0; i top.length(); i++) {
int a=Integer.parseInt(top.charAt(i)+"");
int b=Integer.parseInt(xishu[i]);
sum+=a*b;
}
int xiaoyanjisuan=sum%11;
int xiaoyan=Integer.parseInt(id.substring(17, 18));
if(xiaoyanjisuan==xiaoyan){
System.out.println("身份證號碼檢測正確!");
}else{
System.out.println("身份證號碼檢測錯誤!");
return;
}
String dizhi=id.substring(0,6);
String nian=id.substring(6,10);
String yue=id.substring(10, 12);
String ri=id.substring(12,14);
int sex=Integer.parseInt(id.substring(16,17));
System.out.println("地址碼為:"+dizhi);
System.out.println("生日為:"+nian+"年"+yue+"月"+ri+"日");
if(sex%2==0){
System.out.println("性別為:女");
}else{
System.out.println("性別為:男");
}
}
System.out.println("請輸入你的身份證號碼");
String ID=input.next();
if(ID.lenth()==18){
System.out.println("輸入正確");
}else{
System.out.println("輸入錯誤");
}
18為身份證號碼的第17位數(shù)字表示性別:奇數(shù)表示男性,偶數(shù)表示女性
所有代碼中,只要截取出第17位的數(shù)字出來就可以判斷性別,如:
String sex;
// 判斷性別
if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {
sex = "女";
} else {
sex = "男";
}
我當初用struts1和hibernate做了個簡單的!主要的邏輯代碼在下面!數(shù)據(jù)庫里號碼對應的城市在網(wǎng)上可以查的到
package dao;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Expression;
import entity.Shenfenzhen;
public class IdDao {
private static Configuration configuration = new Configuration().configure() ;
private static SessionFactory sessionFactory =configuration.buildSessionFactory();
private static Session session = null;
public Info query(String s){
Info info = null;
StringBuffer stringBuffer = new StringBuffer(s);
if(s.length()==18){
String end = stringBuffer.substring(17);
//System.out.println(end);
if(!end.matches("\\d||x")){
return null;
}
}
else if(s.length()==15){
stringBuffer.insert(6, "19");
stringBuffer.append("3");
}
int i = Integer.parseInt(stringBuffer.substring(0, 6));
//System.out.println(i);
session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Shenfenzhen.class);
criteria.add(Expression.eq("num", i));
ListShenfenzhen list = criteria.list();
String address = null;
try {
address = list.get(0).getAddress();
}catch (Exception e) {
return null;
}
i = Integer.parseInt(stringBuffer.substring(16,17));
String sex = null;
if(i%2==0){
sex = "女";
}else{
sex = "男";
}
//System.out.println(i);
stringBuffer = new StringBuffer(stringBuffer.substring(6, 14));
// stringBuffer.insert(4, "-");
// stringBuffer.insert(7, "-");
//System.out.println(stringBuffer);
String ss = stringBuffer.toString();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String brithday="";
int year=0;
int month =0;
int day =0;
try {
Date date = dateFormat.parse(ss);
//System.out.println(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH)+1;
day = calendar.get(Calendar.DAY_OF_MONTH);
brithday = (String)(year + "年" + month + "月" + day + "日");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
info = new Info(address, brithday, sex);
Calendar c = Calendar.getInstance();
int nowYear = c.get(Calendar.YEAR);
int nowMonth = c.get(Calendar.MONTH);
int nowDay = c.get(Calendar.DAY_OF_MONTH);
if(nowYearyear){
info = null;
}else if(nowYear ==year nowMonthmonth ){
info = null;
}else if(nowYear == year nowMonth==month nowDayday){
info = null;
}
closeSession(session);
return info;
}
// public static void main(String[] args){
// IdDao dao = new IdDao();
// dao.query("342522998711224513");
// System.out.println("x".matches("\\d||x"));
// }
private void closeSession(Session session){
if(session !=null){
session.close();
}
}
}
public class admin {
static boolean ID_number(String ID_number)
{
if(ID_number.length()==15)
{
for(int i=0;iID_number.length();i++)
{
if(Character.isDigit(ID_number.charAt(i)))
{
}
else
{
return false;
}
}
}else if(ID_number.length()==17)
{
for(int i=0;iID_number.length();i++)
{
if(Character.isDigit(ID_number.charAt(i)))
{
}
else
{
if(i==16ID_number.charAt(i)=='X')
{
}
else
{
return false;
}
}
}
}
else
{
System.out.println("輸入的身份證號碼有誤");
return false;
}
return true;
}
public static void main(String[] args) {
System.out.println(ID_number("1111111111111111X"));
}}
如果只要判斷有非法的字符(除0-9和Xx外)可用正則表達式publicstaticvoidmain(String[]args){//TODOcodeapplicationlogichereStrings="2142213weqrwe32";StringregEx="[^0-9Xx]";Patternpat=Pattern.compile(regEx);Matchermat=pat.matcher(s);booleanrs=mat.find();if(rs){System.out.print("有非法字符");}另外,校驗身份證號碼有專門程序的,可直接校驗身份證號是否正確,在自己在網(wǎng)上找下