题目:利用Calendar类计算自己的出生日期距今天多少天,再将自己的出生日期利用SimpleDateFormat类设定的格式输出显示。
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Scanner; public class birthday { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); System.out.println("请输入你的出生年月日:"); Scanner scan = new Scanner(System.in); int year = scan.nextInt(); int month = scan.nextInt(); int date = scan.nextInt(); calendar.set(year, month-1, date); Calendar now = Calendar.getInstance(); SimpleDateFormat matter=new SimpleDateFormat("距离yyyy年MM月dd日"); System.out.println(matter.format(calendar.getTimeInMillis())); System.out.println("你已经出生了" + ((now.getTimeInMillis() - calendar.getTimeInMillis()) / (1000 * 60 * 60 * 24)) + "天"); } }
运行结果