通过身份证获取地区、年龄、生日

通过身份证获取地区、年龄、生日

package idea;

import java.time.LocalDate;
import java.time.Period;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;

/**
 * 
 * IdCardGetBirthDay [解析身份证号]
 * 
 * @author RunBrick <zhouwei9280@163.com>
 * @version 1.0.0
 * 
 * @since 1.8
 */
public class IdCardGetBirthDay {

	@Test
	public void test() {
                // 身份证号,写自己的即可
		String idCard = "111111111111111111";
		String getBirthDay = dateOfBirth(idCard).toString();
		System.out.println(getBirthDay);
		System.out.println(areas(idCard));
		System.out.println(sex(idCard));
	}

	// 第一位数字:大区
	private static List<String> regionalList = null;
	// 前三位数字:地区
	private static Map<Integer, String> provincesList = new HashMap();

	static {
		// 华中和华南可以并为中南
		regionalList = Arrays.asList("", "华北", "东北", "华东", "华中", "中南", "西南", "西北", "特别行政区");
		// 来源百度百科:https://baike.baidu.com/item/居民身份证号码/3400358?fromtitle=身份证号码&fromid=2135487
		// 华北
		provincesList.put(110, "北京市");
		provincesList.put(120, "天津市");
		provincesList.put(130, "河北省");
		provincesList.put(140, "山西省");
		provincesList.put(150, "内蒙古自治区");
		// 东北
		provincesList.put(210, "辽宁省");
		provincesList.put(220, "吉林省");
		provincesList.put(230, "黑龙江省");
		// 华东地区
		provincesList.put(310, "上海市");
		provincesList.put(320, "江苏省");
		provincesList.put(330, "浙江省");
		provincesList.put(340, "安徽省");
		provincesList.put(350, "福建省");
		provincesList.put(360, "江西省");
		provincesList.put(370, "山东省");
		// 华中地区
		provincesList.put(410, "河南省");
		provincesList.put(420, "湖北省");
		provincesList.put(430, "湖南省");

		// 华南地区
		provincesList.put(440, "广东省");
		provincesList.put(450, "广西壮族自治区");
		provincesList.put(460, "海南省");
		// 西南地区
		provincesList.put(500, "重庆市");
		provincesList.put(510, "四川省");
		provincesList.put(520, "贵州省");
		provincesList.put(530, "云南省");
		provincesList.put(540, "西藏自治区");
		// 西北地区
		provincesList.put(610, "陕西省");
		provincesList.put(620, "甘肃省");
		provincesList.put(630, "青海省");
		provincesList.put(640, "宁夏回族自治区");
		provincesList.put(650, "新疆维吾尔自治区");
		// 特别地区:
		provincesList.put(710, "台湾省");
		provincesList.put(810, "香港特别行政区");
		provincesList.put(820, "澳门特别行政区");
	}

	/**
	 * 
	 * splitIdCard [分割字符串]
	 *
	 * @author: RunBrick <zhouwei9280@gmail.com>
	 * @version v1.0
	 *
	 * @param waitSplitString 等待分割的字符串
	 * @param beginIndex      开始索引
	 * @param length          获取长度
	 * @return 分割后的字符串
	 */
	public String splitIdCard(String waitSplitString, int beginIndex, int length) {
		String string = "";
		string = waitSplitString.substring(beginIndex, beginIndex + length);
		return string;
	}

	/**
	 * 
	 * dateOfBirth [获取生日和年龄]
	 * 
	 * @author: RunBrick <zhouwei9280@gmail.com>
	 * @version v1.0
	 *
	 * @param idCard
	 * @return 年龄和生日
	 */
	public Map<String, String> dateOfBirth(String idCard) {
		Map<String, String> map = new HashMap<String, String>();

		String birthDay = splitIdCard(idCard, 6, 8);

		// 获取生日当天的日期正确格式
		LocalDate birthToDay = LocalDate.of(Integer.valueOf(splitIdCard(birthDay, 0, 4)),
				Integer.valueOf(splitIdCard(birthDay, 4, 2)), Integer.valueOf(splitIdCard(birthDay, 6, 2)));
		map.put("出生日期", birthToDay.toString());
		// 获取今天的日期
		LocalDate toDay = LocalDate.now();
		// 比较两天差的天数,获取年龄
		Period age = Period.between(toDay, birthToDay);
		map.put("年龄", String.valueOf(Math.abs(age.getYears())));

		return map;
	}

	/**
	 * 
	 * areas [通过身份证获取地区信息]
	 *
	 * @author: RunBrick <zhouwei9280@gmail.com>
	 * @version v1.0
	 *
	 * @param idCard
	 * @return
	 */
	public Map<String, String> areas(String idCard) {
		HashMap<String, String> result = new HashMap<String, String>();

		String idCardRegionalNo = splitIdCard(idCard, 0, 1);
		result.put("所处大区", regionalList.get(Integer.valueOf(idCardRegionalNo)));

		String idCardProvincesNo = splitIdCard(idCard, 0, 3);
		result.put("所在地区", provincesList.get(Integer.valueOf(idCardProvincesNo)));
		return result;

	}

	/**
	 * 
	 * sex [判断性别]
	 *
	 * @author: RunBrick <zhouwei9280@gmail.com>
	 * @version v1.0
	 *
	 * @param idCard
	 * @return
	 */
	public String sex(String idCard) {
		String sexNum = splitIdCard(idCard, 16, 1);
		// 判断是否为奇数
		if (Integer.valueOf(sexNum) % 2 == 1) {
			return "男";
		}

		return "女";
	}

}

# Java 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×