Find the number of days ago of this java.util.Date object from the current date

What is the most standard, efficient way to find out how many days ago a particular java.util.Date object appeared? Ideally, I want to return a double representation of a (potentially) fractional number of days ago.

+4
source share
2 answers

It sounds amazing:

(System.currentTimeMillis() - date.getTime()) / (24 * 60 * 60 * 1000d); 

In other words, find out the difference between the current time and the given date in milliseconds, and then divide by the number of milliseconds per day. I explicitly made 1000d a double literal to do the final division in double arithmetic.

+4
source

Get System.currentTimeMillis() and find the Date. Then we get day , month , year . Now you need to easily find the difference.

+1
source

Source: https://habr.com/ru/post/1338037/


All Articles