写串代码从身份证号中提取身份证信息(IDInfo.java)。 编程要求 输入18位身份证号,输出性别、出生年月(格式为××××年××月××日)等信息。身份证号前四位代表所在城市,请判断所在城市。
时间: 2024-06-09 10:07:03 浏览: 176
以下是一个简单的实现:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class IDInfo {
private static final String[] cityCodes = {
"1101", "1201", "3101", "5001", "1301", "1401", "1501", "2101", "2201", "2301",
"3100", "3201", "3301", "3401", "3501", "3601", "3701", "4101", "4201", "4301",
"4401", "4501", "4601", "5000", "5101", "5201", "5301", "5401", "6101", "6201",
"6301", "6401", "6501", "7101", "8101", "8201"
};
public static String getGender(String idNumber) {
String gender = "";
char c = idNumber.charAt(idNumber.length() - 2);
if (c % 2 == 0) {
gender = "女";
} else {
gender = "男";
}
return gender;
}
public static String getBirthday(String idNumber) {
String birthday = "";
String year = idNumber.substring(6, 10);
String month = idNumber.substring(10, 12);
String day = idNumber.substring(12, 14);
birthday = year + "年" + month + "月" + day + "日";
return birthday;
}
public static String getCity(String idNumber) {
String city = "";
String code = idNumber.substring(0, 4);
for (int i = 0; i < cityCodes.length; i++) {
if (code.equals(cityCodes[i])) {
city = getCityName(i);
break;
}
}
return city;
}
private static String getCityName(int index) {
String[] cityNames = {
"北京市", "天津市", "上海市", "重庆市", "河北省", "山西省", "内蒙古自治区", "辽宁省", "吉林省", "黑龙江省",
"上海市", "江苏省", "浙江省", "安徽省", "福建省", "江西省", "山东省", "河南省", "湖北省", "湖南省",
"广东省", "广西壮族自治区", "海南省", "重庆市", "四川省", "贵州省", "云南省", "西藏自治区", "陕西省", "甘肃省",
"青海省", "宁夏回族自治区", "新疆维吾尔自治区", "台湾省", "香港特别行政区", "澳门特别行政区"
};
return cityNames[index];
}
public static void main(String[] args) {
String idNumber = "310101199001010011";
String gender = getGender(idNumber);
String birthday = getBirthday(idNumber);
String city = getCity(idNumber);
System.out.println("性别:" + gender);
System.out.println("生日:" + birthday);
System.out.println("城市:" + city);
}
}
```
这个程序中,我们定义了三个静态方法 `getGender`、`getBirthday` 和 `getCity`,分别用于解析性别、出生日期和所在城市信息。在 `main` 方法中,我们演示了如何使用这三个方法来解析身份证号码信息。
阅读全文