Get num days between 2 Calendars

I have two Calendar objects, with their time fields (hour, minute, second, millisecond) all set to zeros. I want to get the days difference between these two objects.

Example:

Calendar 1: 2006-06-26
Calendar 2: 2006-06-27
results: -1

Calendar 1: 2006-06-26
Calendar 2: 2006-06-25
results: 1
LVL 9
yongsingAsked:
Who is Participating?
 
mukundha_expertConnect With a Mentor Commented:
sorry am not sure about compareTo(),

try this

long cal1time = cal.getTimeinMillis()
long cal2time = cal2.getTimeinMillis()

long diff = cal1time - cal2time;

int days = diff / 1000*60*60*24
0
 
mukundha_expertCommented:
Use compareTo() to get the difference in time in Milliseconds,

from this you can get the difference of days,

int time = cal1.compareTo(cal2);

int days = time / 1000*60*60*24

0
 
Mayank SAssociate Director - Product EngineeringCommented:
0
 
Mayank SAssociate Director - Product EngineeringCommented:
0
All Courses

From novice to tech pro — start learning today.