重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
public static void main(String[] args){
你所需要的网站建设服务,我们均能行业靠前的水平为你提供.标准是产品质量的保证,主要从事网站设计、做网站、企业网站建设、成都做手机网站、网页设计、高端网站设计、网页制作、做网站、建网站。成都创新互联拥有实力坚强的技术研发团队及素养的视觉设计专才。
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位数字表示性别:奇数表示男性,偶数表示女性
所有代码中,只要截取出第17位的数字出来就可以判断性别,如:
String sex;
// 判断性别
if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {
sex = "女";
} else {
sex = "男";
}
我当初用struts1和hibernate做了个简单的!主要的逻辑代码在下面!数据库里号码对应的城市在网上可以查的到
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("有非法字符");}另外,校验身份证号码有专门程序的,可直接校验身份证号是否正确,在自己在网上找下