How to calculate the date difference in Android?
Asked
Active
Viewed 2,908 times
0
-
1can you give an example of what you mean by "the" date difference? – Macarse Aug 13 '10 at 02:45
-
1As @Marcare states you need to be more clear. If you just want to know the number of ms between 2 dates just subtract the Date.getTime() values. – seand Aug 13 '10 at 03:25
1 Answers
2
If you can't afford 3rd party libraries like JodaTime, then your best bet is really java.util.Calendar
. You can use the Calendar#add()
method in a loop to calculate the difference in years, months and days between two instances. Then to calculate the difference in hours, minutes and seconds, just do the usual math on Calendar#getTimeInMillis()
.
Long story short, I've posted a basic example before here. You may find it useful.