提取身份证号中的出生日期

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日

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值