1

Hi have two dates in date format, how do i get the difference of days between the two ?

Date date1;
Date date2 ;
int numberDays = ?
Ajay S
  • 48,003
  • 27
  • 91
  • 111
user2133558
  • 524
  • 2
  • 10
  • 29
  • 2
    None of the duplicated questions addresses days between two `Date`s – Steve Kuo Mar 21 '13 at 20:12
  • import org.joda.time.Days; import org.joda.time.LocalDate; (...) int numberDays = Days.daysBetween(new LocalDate(date1), new LocalDate(date2)) – plancys Dec 16 '14 at 10:12

1 Answers1

-4

The recommendation is to use JodaTime API for Dates:

import java.util.logging.Logger;

import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Interval;

public class DatesInterval {
    private final static Logger log = Logger.getLogger(DatesInterval.class.getName());
    public static void main(String[] args) {
        //creates a date 10 days ago in JodaTime
        DateTime daysAgo10 = new DateTime().minusDays(10);
        //today
        DateTime today = new DateTime();

        //create an interval in Joda
        Interval interval = new Interval(daysAgo10.getMillis(), today.getMillis());
        //than get the duration
        Duration duration = interval.toDuration();

        //now you can get what you want. As you can imagine you can get days, millis, whateaver you need. 
        log.info("Difference in days: " + duration.getStandardDays());
    }
}

http://joda-time.sourceforge.net/

regards.

groo
  • 4,213
  • 6
  • 45
  • 69
  • 1
    I'm sure there was a copy-pasteable answer for this one but if the question has been thoroughtly answered somewhere else, mark it as duplicate and link the proper one. In other case, link only answers are considered too poor: add at least a bit of context to your link. – ffflabs Oct 07 '14 at 10:57
  • @amenadiel - ok amenadiel, got it. will fix this and do it nex time. Thanks. – groo Oct 07 '14 at 10:59