Calculate days between two dates in Java

I have a code that has 2 dates in the format

DateFormat dateFormat= new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
           Date date=new Date();

           DateFormat formatter ; 
           Date publishDate = (Date)dateFormat.parse(pubDate);  

I want to calculate the days between two dates. I can not use the Joda package. Is there a way to get the difference on the day in 2 dates?

+3
source share
2 answers

Date d1 = ..., d2 = ...;
long t1 = d1.getTime(),
   t2 = d2.getTime();
long day = 1000 * 60 * 60 * 24; // milliseconds in a day
return (t1 - t2) / day;
+4
source

You can simply subtract millions between two dates and then divide by the number of milliseconds per day

+1
source

All Articles