I saw all the post in here and still I can’t figure how do get difference between two android dates.
This is what I do:
long diff = date1.getTime() - date2.getTime();
Date diffDate = new Date(diff);
and I get: the date is Jan. 1, 1970 and the time is always bigger in two hours…I’m from Israel so the two hours is timeOffset.
How can I get normal difference???
You’re close to the right answer, you are getting the difference in milliseconds between those two dates, but when you attempt to construct a date out of that difference, it is assuming you want to create a new
Dateobject with that difference value as its epoch time. If you’re looking for a time in hours, then you would simply need to do some basic arithmetic on thatdiffto get the different time parts.Java:
Kotlin:
All of this math will simply do integer arithmetic, so it will truncate any decimal points