java.util.concurrent.TimeUnit: convert between milliseconds and days : Timing « Development Class « Java

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

1.Compute and display elapsed time of an operation
2.Get system time using System class
3.Class for program event timing
4.Get elapsed time in milliseconds
5.Get elapsed time in seconds
6.Get elapsed time in minutes
7.Get elapsed time in hours
8.Get elapsed time in days
9.Get time in milliseconds using Java Calendar
10.Get seconds since