計算兩個日期之差異

摘要:計算兩個日期之差異

 

01 import java.util.Calendar;
02
03 public class DateDifferent{   
04    public static void main(String[] args){
05    Calendar calendar1 = Calendar.getInstance();
06    Calendar calendar2 = Calendar.getInstance();
07    calendar1.set(2007, 01, 10);
08    calendar2.set(2007, 07, 01);
09    long milliseconds1 = calendar1.getTimeInMillis();
10    long milliseconds2 = calendar2.getTimeInMillis();
11    long diff = milliseconds2 - milliseconds1;
12    long diffSeconds = diff / 1000;
13    long diffMinutes = diff / (60 * 1000);
14    long diffHours = diff / (60 * 60 * 1000);
15    long diffDays = diff / (24 * 60 * 60 * 1000);
16    System.out.println("\nThe Date Different Example");
17    System.out.println("Time in milliseconds: " + diff + " milliseconds.");
18    System.out.println("Time in seconds: " + diffSeconds + " seconds.");
19    System.out.println("Time in minutes: " + diffMinutes + " minutes.");
20    System.out.println("Time in hours: " + diffHours + " hours.");
21    System.out.println("Time in days: " + diffDays + " days.");
22  }
23 }