輸入日期計算天數
阿新 • • 發佈:2019-01-08
package ch3; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class Text { public static void main(String[] args) throws ParseException { Scanner s = new Scanner(System.in); System.out.println("請輸入年月日(2013-2-10):"); String str =s.next(); Date date = new Date(); long a = date.getTime(); SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); long b = sd.parse(str).getTime(); int success = (int) ((a-b)/(1000*60*60*24)); //1000毫秒*60分鐘*60秒*24小時 = 天 System.out.print("距離輸入的的年月日有:"+success+"天"); } }