题目:利用Calendar类计算自己的出生日期距今天多少天,再将自己的出生日期利用SimpleDateFormat类设定的格式输出显示。
1.代码
1 package Eg; 2 3 import java.util.Calendar; 4 import java.util.Date; 5 import java.util.Scanner; 6 import java.text.SimpleDateFormat; 7 8 public class Ab1 { 9 10 11 public static void main(String[] args) { 12 Date nowTime =new Date(); 13 14 Scanner reader = new Scanner(System.in); 15 System.out.println("请输入您的出生日期"); 16 int year =reader.nextInt(); 17 int month =reader.nextInt(); 18 int day =reader.nextInt(); 19 Calendar calendar=Calendar.getInstance(); 20 Calendar calendar2=Calendar.getInstance(); 21 calendar.set(year, month-1, day); 22 calendar2.setTime(nowTime); 23 System.out.print("出生日期距今天有"); 24 System.out.print((calendar2.getTimeInMillis()/86400000)-(calendar.getTimeInMillis()/86400000)); 25 System.out.print("天"); 26 27 } 28 29 }
2.运行结果