根据身份证号码计算一个人的生日,在身份证中第7位到14位是出生日期

例如:

输入: 450522199712073639

输出:1997年12月07日

public class Test03 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String  str = "450522199712073639";
		//第一种方法,分别截取年月日并输出
		/*
		 * String year = str.substring(6,10);
		String month = str.substring(10, 12);
		String day = str.substring(12, 14);
		System.out.println(year + "年" + month + "月" + day + "日");
		*/
		//第二种方法,截取整段年月日的数字,最后通过SimpleDateFormat转换而来
		String cno = str.substring(6, 14);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date birthday = null;
		try {
			birthday = sdf.parse(cno);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
		System.out.println(sdf1.format(birthday));
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_39788493/article/details/80728792