How to find out how many days have passed since a certain date?

How do you know how many days have passed since a special date? Which package do I need to use and how?

+5
source share
5 answers

EDIT My previous answer was only valid for a year.

You can use the difference in milliseconds as follows:

Date date1 = // some date
Date date2 = // some other date
long difference = date2.getTime() - date1.getTime();
long differenceDays = difference / (1000 * 60 * 60 * 24);

Basically the same as timbooo, just a shorter way.

+4
source

Only for the protocol - I love java.util.concurrent.TimeUnitthat.

Date d1 = ...
Date d2 = ...
long dif = d1.getTime() - d2.getTime();
long days = TimeUnit.MILLISECONDS.toDays(dif);

, , morja, TimeUnit . , 24, 60 .. , Java Code ( -1, 0 1 ) .

+5

kodejava.org

+2

Jodatime :

Date now = // some Date
Date then = // some Date
int days = Days.daysBetween(new DateTime(now), new DateTime(then)).getDays();
+2

, , getTimeInMillis() ( 1970 ), , 1000/-a- /--/ . ;)

-1

All Articles