Java中自定义当前的系统时间去计算年龄demo

public static void main(String[] args) {
        // 创建出生日期对象
//       获取当前系统时间
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("当前系统时间:" + sdf.format(cal.getTime()));

        // 设置自定义的日期
        cal.set(Calendar.YEAR, 2023);
        cal.set(Calendar.MONTH, Calendar.MARCH);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        System.out.println("自定义当前系统日期:" + sdf.format(cal.getTime()));


        String idCard = "383293196402299012";
        // 截取生日部分
        int birthYear = Integer.parseInt(idCard.substring(6, 10)); // 截取出生年份
        int birthMonth = Integer.parseInt(idCard.substring(10, 12)); // 截取出生月份
        int birthDay = Integer.parseInt(idCard.substring(12, 14)); // 截取出生日期
        int birthDayss = Integer.parseInt(idCard.substring(6, 14)); // 截取出生日期
        System.out.println("该人的生日 = " + birthDayss);
        LocalDate birthDate = LocalDate.of(birthYear, birthMonth, birthDay);
        Period age = Period.between(birthDate, LocalDate.parse(sdf.format(cal.getTime())));
        System.out.println("该人的年龄为:" + age.getYears() + "岁 " + age.getMonths() + "个月 " + age.getDays() + "天");
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@百思不得奇解

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值