1 package com.lovo; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 import java.util.Scanner; 7 8 public class Day { 9 public static void main(String[] args) { 10 final long MILLIS_OF_DAY = 24 * 60 * 60 * 1000; 11 Scanner sc = new Scanner(System.in); 12 System.out.println("输入日期格式为:1-31-1994,表示1994年1月31日"); 13 System.out.print("请输入第一个日期:"); 14 String a1 = sc.nextLine(); 15 System.out.print("请输入第二个日期:"); 16 String a2 = sc.nextLine(); 17 SimpleDateFormat date = new SimpleDateFormat("MM-dd-yyyy"); 18 19 try { 20 Date date1 = date.parse(a1); 21 long s1 = date1.getTime(); 22 try { 23 Date date2 = date.parse(a2); 24 long s2 = date2.getTime(); 25 long s = Math.abs(s1 - s2); 26 System.out.println("两个日期之间相差了" + s / MILLIS_OF_DAY + "天"); 27 } catch (ParseException e) { 28 System.out.println(e.getMessage()); 29 } 30 31 } catch (ParseException e) { 32 System.out.println(e.getMessage()); 33 } 34 35 sc.close(); 36 } 37 }