0

I've seen many examples on Java 7 how to calculate days between 2 dates but I have found out that they do not include leap years correctly.

I've seen this example, but apparently leap years are not included:

Calculating days between two dates with in java

So I would like to see correct Java code how to get number of days without using Joda Time and Java 8?

Community
  • 1
  • 1
DarioBB
  • 663
  • 2
  • 8
  • 29
  • The easiest way would be to convert your Gregorian date to a Julian day number. This is described [on Wikipedia](https://en.wikipedia.org/wiki/Julian_day#Converting_Julian_or_Gregorian_calendar_date_to_Julian_day_number). Once you have the Julian day number for both dates, subtract them. – Andy Turner May 13 '16 at 08:25
  • 1
    No, sorry, the easiest way would be to use Jodatime or Java 8 :) – Andy Turner May 13 '16 at 08:27
  • I mentioned I am interested in Java 7. I am using Java 7 for my work for specific reasons. – DarioBB May 13 '16 at 09:16
  • Use Julian days, and you could do it in any version of Java. – Andy Turner May 13 '16 at 09:41
  • Before java.time framework arrived in Java 8 (and [back-ported to Java 6 & 7](http://www.threeten.org/threetenbp/)), there was no built-in way to truly represent a date-only value sans time-of-day & time zone. For such a value, see the [`java.time.LocalDate`](http://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html) class. So there is no clean simple answer for you using old code, as seen in the comments on the [Answer by tamas rev](http://stackoverflow.com/a/37204352/642706). Those old date-time classes were supplanted by java.time classes for a reason (actually, *many* reasons). – Basil Bourque May 13 '16 at 18:49

1 Answers1

0

What about this arithmetic?

package com.example;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DayDifference {

    public static void main(String[] args) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = sdf.parse("2016-02-28");
        Date d2 = sdf.parse("2016-03-01");

        long diff = (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24);

        System.out.println("diff: " + diff);
    }
}

This outputs 2, so works well with leap years.

Update: According to Andreas's comment you're better of with the +12h hack because it handles the daylight saving too: (d2.getTime() - d1.getTime() + 1000 * 60 * 60 * 12) / (1000 * 60 * 60 * 24)

Community
  • 1
  • 1
Tamas Rev
  • 7,008
  • 5
  • 32
  • 49
  • 2
    But doesn't work when crossing Daylight Saving Time. Says there are `0` days between `2016-03-13` and `2016-03-14` (America/New_York). – Andreas May 13 '16 at 07:58
  • 1
    Since inputs are pure dates, and DST is never 12+ hours, this can be fixed by making the division rounding, i.e. add half a day: `(d2.getTime() - d1.getTime() + 1000 * 60 * 60 * 12) / (1000 * 60 * 60 * 24)` – Andreas May 13 '16 at 08:04
  • That gives '1' on my machine (CET/CEST). Anyway, we need full specification for what do we consider as 'one day difference'. E.g. what should be the difference between `2016-03-13 23:12:05` and `2016-03-14 00:17:44` ? – Tamas Rev May 13 '16 at 08:05
  • If the interpretation is that a "date" doesn't have a time-of-day, i.e. is a "pure" date like your code example has it, then it is a non-issue. – Andreas May 13 '16 at 08:07