java_計算一個人從出生日期開始到他參加高考的時間間隔有多少天(假設是18歲參加高考)

climbexck發表於2020-11-20

package gaokao;
import javax.print.DocFlavor;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class gaokao {
public static void function() throws Exception{
System.out.println(“請輸入出生日期 格式 YYYY-MM-dd: “);
String birthdatString = new Scanner(System.in).next();
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Date birthdayDate =sdf.parse(birthdatString);
String gaokaoString = Integer.toString(Integer.parseInt(birthdatString.split(”-”)[0])+18)+"-06-07";
Date gaokaoDate =sdf.parse(gaokaoString);
long birthdaySecond =birthdayDate.getTime();
long gaokaoSecond =gaokaoDate.getTime();
long second = gaokaoSecond-birthdaySecond;
long tian;
String tian1;
tian =second / 1000 / 60 / 60 / 24;
tian1 =String.valueOf(tian);
System.out.println(“離高考還有”+tian1+“天”);
}
public static void main(String[] args) {
try {
gaokao.function();
}catch(Exception e){
e.printStackTrace();
}
}
}

相關文章