首页 > 代码库 > Java源码,算算你活了多久

Java源码,算算你活了多久

 1 import java.text.*; 2 import java.util.*; 3 public class DateDemo { 4 public static void main(String[] args) throws Exception{ //throws Exception { 5     /*DateFormat df = DateFormat.getDateInstance(); 6     String date ="2014-7-18"; 7     Date d = df.parse(date); 8     System.out.println(d);*/ 9     Scanner sc = new Scanner(System.in);10     System.out.println("请输入你的出生年月日");11     String Date1 = sc.nextLine();12     System.out.println("请输入现在的年月日");13     String Date2 = sc.nextLine();14     DateFormat df = DateFormat.getDateInstance();15     Date de1 = df.parse(Date1);//转换成日期类型16     Date de2 = df.parse(Date2);//转换成日期类型17     long time1 = de1.getTime();//转换成毫秒18     long time2 = de2.getTime();//转换成毫秒19     long time3 = Math.abs(time2-time1)/1000/60/60/24;20     System.out.println(time3);21 }22 }