Java Tutorial/Development/TimeUnit
java.util.concurrent.TimeUnit can be used to 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 ");
}
}
Using new methods in TimeUnit
import java.util.concurrent.TimeUnit;
public class TimeUnitDemo {
public static void main(String[] args) {
TimeUnit tu = TimeUnit.DAYS;
System.out.println(tu.toDays(1));
System.out.println(tu.toHours(1));
System.out.println(tu.toMinutes(1));
}
}