33

Possible Duplicate:
Calculating the Difference Between Two Java Date Instances

I know this might be a duplicate thread. But I am trying to figure out a way to compute the difference between two dates. From jquery the date string is in the format 'yyyy-mm-dd'. I read this as a String and converted to java Date like this

Date d1 = new SimpleDateFormat("yyyy-M-dd").parse((String) request.getParameter(date1));
Date d2 = new SimpleDateFormat("yyyy-M-dd").parse((String) request.getParameter(date2));

I want to compute the difference in the number of days between these two dates.

Note: I cannot use third party API's as those need to reviewed.

Community
  • 1
  • 1
user525146
  • 3,918
  • 14
  • 60
  • 103

3 Answers3

37

Edit 2018-05-28 I have changed the example to use Java 8's Time API:

LocalDate d1 = LocalDate.parse("2018-05-26", DateTimeFormatter.ISO_LOCAL_DATE);
LocalDate d2 = LocalDate.parse("2018-05-28", DateTimeFormatter.ISO_LOCAL_DATE);
Duration diff = Duration.between(d1.atStartOfDay(), d2.atStartOfDay());
long diffDays = diff.toDays();
uldall
  • 2,458
  • 1
  • 17
  • 31
  • 3
    This will fail across spring's daylight savings boundary. Example (using your code), `d1 = new Date(112, 3-1, 11, 0, 0, 0)`, `d2 = new Date(112, 3-1, 12, 0, 0, 0)`, results in `0`. – Steve Kuo Oct 23 '12 at 22:14
  • 1
    The answer provides a truncated result. For a rounded result: long diffDays = (diff + 12 * 60 * 60 * 1000) / (24 * 60 * 60 * 1000) – Giorgio Barchiesi Nov 14 '13 at 10:47
  • @SteveKuo, I know this was a while ago, but do you have a better solution that doesn't fail for daylight savings? I'm not trying to be snarky, I am working on a similar issue myself and would prefer to avoid code with obvious flaws. – A Bailey Jun 19 '17 at 19:35
  • 1
    This failed for me with exception being: `java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds`. What worked for me was: `d1.until(d2).getDays()`, where both `d1` and `d2` are created as `LocalDate.of(...)`. – G. Urikh Jul 31 '20 at 12:43
13

Assuming that you're constrained to using Date, you can do the following:

Date diff = new Date(d2.getTime() - d1.getTime());

Here you're computing the differences in milliseconds since the "epoch", and creating a new Date object at an offset from the epoch. Like others have said: the answers in the duplicate question are probably better alternatives (if you aren't tied down to Date).

mnajera
  • 490
  • 4
  • 9
  • Please read the question again carefully. Your answer does **not** answer the original question. – DavidPostill Mar 15 '15 at 10:34
  • 1
    doesn't matter because it helps when you are looking for solutions for yourself... This is the answer i will use for my own issue – Felipe Aug 02 '20 at 13:02
7
Date d1 = new SimpleDateFormat("yyyy-M-dd").parse((String) request.
            getParameter(date1));
Date d2 = new SimpleDateFormat("yyyy-M-dd").parse((String) request.
            getParameter(date2));

long diff = d2.getTime() - d1.getTime();

System.out.println("Difference between  " + d1 + " and "+ d2+" is "
        + (diff / (1000 * 60 * 60 * 24)) + " days.");
16dots
  • 2,941
  • 1
  • 13
  • 15
  • 2
    Will fail across the start of daylight savings boundary. Example, d1=`2012-3-11` and d2=`2012-3-12` results in 0 days. – Steve Kuo Oct 23 '12 at 22:14