根据输入的日期计算你活了多少天(新手用于记录每天的作业)

//导入的包。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
//创建的一个类。
public class zy2ri0319 {
  //公共静态的主方法。
public static void main(String[] args)throws ParseException{
    //调用方法。
lx();
}
//创建方法。
  //throws ParseException(抛出异常声明,未讲)
public static void lx()throws ParseException{
//打印提示。
System.out.println("请您输入您的出生日期");
//扫描控制台输入的值。
Scanner sc = new Scanner(System.in);
//获取用户输入的值。
String s = sc.next();
//创建时间格式对象并写入格式。
SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd");
//创建对象,使用户输入的值可以转换成时间格式。
Date date = s1.parse(s);
//把输入的时间转为毫秒格式。
long time1 = date.getTime();
//获取现在的时间并且转换为毫秒。
long time2 = new Date().getTime();
//毫秒除于1000变成秒,秒除于60等于分钟,分钟除于60变成小时,小时除于24变成天。
long day = (time1-time2)/1000/60/60/24;
//打印结果。
System.out.println(day);
}
}

猜你喜欢

转载自www.cnblogs.com/lxr521/p/10561595.html