1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import java.util.Calendar; public class DateDifferenceExample{ public static void main(String[] args){ //get instance of Calendar objects Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); //set two dates we want to know difference of cal1.set(2006, 12, 30); cal2.set(2007, 5, 3); long milis1 = cal1.getTimeInMillis(); long milis2 = cal2.getTimeInMillis(); //difference in milliseconds long diff = milis2 - milis1; //difference in seconds long diffSeconds = diff / 1000; //difference in minutes long diffMinutes = diff / (60 * 1000); //difference in hours long diffHours = diff / (60 * 60 * 1000); //difference in days long diffDays = diff / (24 * 60 * 60 * 1000); System.out.println("Date difference in milliseconds: " + diff + " milliseconds."); System.out.println("Date difference in seconds: " + diffSeconds + " seconds."); System.out.println("Date difference in minutes: " + diffMinutes + " minutes."); System.out.println("Date difference in hours: " + diffHours + " hours."); System.out.println("Date difference in days: " + diffDays + " days."); } } /* Output of above date difference program would be Date difference in milliseconds: 10713600000 milliseconds. Date difference in seconds: 10713600 seconds. Date difference in minutes: 178560 minutes. Date difference in hours: 2976 hours. Date difference in days: 124 days. */ |
It's all about Java development. A place mainly for java programmers.
Tuesday, 13 March 2012
Calculate difference in days between two dates
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment