Vivek Dhiman Vivek Dhiman - 5 months ago 9
Java Question

Time difference between two calander dates

I am looking for to calculate the time difference between two calendar dates and need the result difference in minutes. Below is what I am trying but it says: can't apply - operator. My

page.getLastModified().getTime()
gives me Calendar object. Any idea how can I achieve this.

long lastModified = page.getLastModified().getTime() - Calendar.getInstance().getTime()
;

Thanks

Answer

getTime() returns Date instance and there is no overloaded - operator for Date. You may try with getTimeInMillis() to get difference milliseconds between two dates. You can later convert these milliseconds to minutes for instance with

TimeUnit.MILLISECONDS.toMinutes(durationInMillis)