java.util.concurrent.TimeUnit: convert between milliseconds and days
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
public class Main {
public static long getDifference(Calendar a, Calendar b, TimeUnit units) {
return units.convert(b.getTimeInMillis() - a.getTimeInMillis(), TimeUnit.MILLISECONDS);
}
public static void main(String[] args) {
Calendar first = Calendar.getInstance();
first.set(2008, Calendar.AUGUST, 1);
Calendar second = Calendar.getInstance();
System.out.println(getDifference(first, second, TimeUnit.DAYS) + " day(s) between ");
}
}
Related examples in the same category