long diff = milliseconds2 - milliseconds1;
but it gets the difference based on 12 hr clock so it returns negative when send time is prior to 1st time while calculating the difference. so what is the modification required in order to get proper time difference value without negative value.
Thank youMark February 23, 2012 at 2:05 PM
saved me the time of developing an algorithm
Date Difference ProblemArun R T April 3, 2012 at 2:33 PM
In your code import java.util.Calendar; public class DateDifferent{ public static void main(String[] args){ Calendar calendar1 = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); calendar1.set(2007, 01, 10); calendar2.set(2007, 07, 01); long milliseconds1 = calendar1.getTimeInMillis(); long milliseconds2 = calendar2.getTimeInMillis(); long diff = milliseconds2 - milliseconds1; long diffSeconds = diff / 1000; long diffMinutes = diff / (60 * 1000); long diffHours = diff / (60 * 60 * 1000); long diffDays = diff / (24 * 60 * 60 * 1000); System.out.println("\nThe Date Different Example"); System.out.println("Time in milliseconds: " + diff + " milliseconds."); System.out.println("Time in seconds: " + diffSeconds + " seconds."); System.out.println("Time in minutes: " + diffMinutes + " minutes."); System.out.println("Time in hours: " + diffHours + " hours."); System.out.println("Time in days: " + diffDays + " days."); } } I substitute calendar1.set(2012, 03, 31); calendar2.set(2012, 04, 03); Time in days: 2 days. "But current is 3"
Does not workNot May 1, 2012 at 4:09 PM
It does not work correctly from month to month!
what is the modification required?pooja September 5, 2012 at 3:45 PM
long diff = milliseconds2 - milliseconds1; but it gets the difference based on 12 hr clock so it returns negative when send time is prior to 1st time while calculating the difference. so what is the modification required in order to get proper time difference value without negative value.
Post your Comment