0

How to calculate the date difference in Android?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
User358218
  • 523
  • 3
  • 8
  • 25
  • 1
    can you give an example of what you mean by "the" date difference? – Macarse Aug 13 '10 at 02:45
  • 1
    As @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 Answers1

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555