Rahul Devanavar Rahul Devanavar - 2 months ago 10
Java Question

Find difference between two timestamps

I want to find differences between given timestamp and current system time.

Answer Source

Try this method:

public static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit)
    long diffInMillies = Math.abs(date2.getTime() - date1.getTime());
    return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS);

It gives you the difference of whatever unit (days, years, ...).

long timestamp1 = 1500462813000;
long timestamp2 = System.currentTimeMillis();
long diffInDays = getDateDiff(new Date(timestamp1 ), new Date(timestamp2), TimeUnit.DAYS);