class Birthday{ //定义日期类
String year;
String month;
String date;
public void show(){ //输出日期信息
System.out.println(year + "年" + month +"月" + date + "日");
}
}
public class Demo {
//getBrithday()方法是从身份证号id中提取出生日期信息
public static Birthday getBirthday(String id){
Birthday bir = new Birthday(); //日期对象
if(18 != id.length()) //如果字符串中没有18个字符
return null;
bir.year = id.substring(6,10); //subString成员方法提取第7~10位置的子串
bir.month = id.substring(10,12); //subString成员方法提取第11~12位置的子串
bir.date = id.substring(12,14); //subString成员方法提取第13~14位置的子串
return bir;
}
public static void main(String args[]){ //调用Demo类成员方法getBirthday(),取得日期对象
Birthday birth = getBirthday("440303197812180001");
birth.show(); //调用日期对象的show()方法
}
}
输出为:
1978年12月18日