#提取身份证号中的出生日期:
package demo;
class Birthday {
String year;
String month;
String date;
public void show() {
System.out.print(year + "年" + month + "月" + date + "日");
}
}
public class Chapter0607 {
//getBirthday()方法从身份证号id中提取出生日期信息
public static Birthday getBirthday(String id) {
if (18 != id.length()) {//如果字符串没有18个字符
return null;
}
Birthday bir = new Birthday();
bir.year = id.substring(6,10);
bir.month = id.substring(10,12);
bir.date = id.substring(12,14);
return bir;
}
public static void main(String[] args) {
Birthday birth = getBirthday("440303197812180001");
birth.show();//调用日期对象的show()方法
}
}