日期时间工具类小练习(计算你活了多少天)

日期时间工具类小练习
根据日期时间工具类计算,从你出生到今天一共经历了多少天


public class Birthday {
    //根据 日期时间 工具类 计算 . 从 你出生到今天  一共经历多少天
    public static void main(String[] args) throws ParseException {
        Calendar calendar = Calendar.getInstance();   // 声明日历对象
        Scanner sc = new Scanner(System.in);//手动输入出生年月日并获取输入内容
        System.out.println("请输入您的出生年份:");
        int year = sc.nextInt();
        System.out.println("请输入您的出生月份:");
        int month = sc.nextInt();
        System.out.println("请输入您的出生日期:");
        int day = sc.nextInt();
        calendar.set(year, month-1, day);    // 将输入年、月、日放到日历中

        long olddate = calendar.getTimeInMillis();    //获得日历时间的毫秒值
        long nowdate = System.currentTimeMillis();    //获得当前时间的毫秒值
        long l = (nowdate - olddate) / 1000 / 60 / 60 / 24;  
         //将两值相减,再转换为天数   毫秒/分钟/小时/天
        System.out.println("您目前活了" + l + "天!");
    }
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值