计算从出生到现在一共活了多少天?

2022-07-27,,

JavaSE基础题 api方法的使用

计算出生到现在一共活了多少天?

package com.it.fxp.Demo01.demo04;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/计算从出生到现在一共活了多少天?/
public class SimplDateFormatTest {
public static void main(String[] args) throws ParseException {
String birthday=“2000-12-25”;//出生日期
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd”);

    //当前时间和出生时间类型不一致,需转换类型long
    Date birthdayDate=sdf.parse(birthday);//出生日期的Date对象
    Date nowDate = new Date();//当前日期的Date对象

    long birthdayTime=birthdayDate.getTime();//出生日期的毫秒值
    long nowTime=nowDate.getTime();//当前日期的毫秒值

    long time=(nowTime-birthdayTime)/1000/60/60/24;//计算天数
    System.out.println("活了"+time+"天");


}

}

本文地址:https://blog.csdn.net/m0_51383338/article/details/110005738

《计算从出生到现在一共活了多少天?.doc》

下载本文的Word格式文档,以方便收藏与打印。