0

I have written a code to calculate number of days between dates in following way

SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");

Date date1 = date.parse(startDate);
Date date2 = date.parse(endDate);

long difference = (date2.getTime()-date1.getTime())/(24*60*60*1000);

My job is to find whether term between startDate and endDate is exactly one year.

i am initially checking year type(whether leap year/normal year) after calculating difference between dates.

if non leap year, then i will check whether difference is 365

if leap year, then i will check whether difference is 366.

But for few dates in november say

startDate = 11/04/2015(MM/dd/yyyy) endDate = 11/04/2016(MM/dd/yyyy)

difference is calculated as 365 where code is expecting 366 as endDate year 2016 is a leap year and also end date's month is after february.

What really happening is absolute difference we are getting is 365.9 but not 366.0

This is happening for only few dates of november as per my observations.

11/02/2015 to 11/02/2016, 11/03/2015 to 11/03/2016 , 11/04/2015 to 11/04/2016, 11/05/2015 to 11/05/2016,11/06/2015 to 11/06/2016.

For remaining i am seeing difference as 366.0.

My question is, Why this peculiar behavior we are seeing for these few dates only. What is the problem with date.getTime when it always returns milliseconds passed since January 1, 1970, 00:00:00 GMT.

pbaris
  • 4,525
  • 5
  • 37
  • 61
  • You may need to user Math.round() before assigning to long. Or use Joda time. http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances – Roman Pustylnikov Oct 14 '15 at 07:00

3 Answers3

3

Use a DateTime API. Since Java 8 got this you should use it like this:

private static boolean isYearBetween(String start, String end) {
    DateTimeFormatter pattern = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    LocalDate startDate = LocalDate.parse(start, pattern);
    LocalDate endDate = LocalDate.parse(end, pattern);

    Period between = Period.between(startDate, endDate).normalized();

    return Period.ofYears(1).equals(between.isNegative() ? between.negated() : between);
}
Flown
  • 11,480
  • 3
  • 45
  • 62
  • Thank you for introducing java8 Date Time API to me. But here my code should run through java 6 only. and i also want to know, why this behavior is happening for few mentioned dates only. – user3173392 Oct 14 '15 at 07:29
  • 1
    @user3173392 I think there is no clean way in < Java 8 to calculate the period between two `Date`s. As you can see you have many special cases with leap years. Like if the end date is in a leap year is the date after the 29th February then use 366 days else 365 to compare (same with the start date). You should consider using [Joda Time](http://www.joda.org/joda-time/). – Flown Oct 14 '15 at 10:19
0

Why not use the new feature that comes with java 8: java.time.LocalDateTime? You can then easily format your dates and time with java.time.format.DateTimeFormatter. For leap years as well, it works better than java.Date.

LocalDate date = LocalDate.parse("yyyy-MM-dd");
LocalDate date1 = date.parse(startDate);
LocalDate date2 = date.parse(endDate);
Helenesh
  • 3,999
  • 2
  • 21
  • 36
0

I run your code, I got 366

    SimpleDateFormat date = new SimpleDateFormat("MM/dd/yyyy");
    Date date1 = date.parse("11/02/2015");
    Date date2 = date.parse("11/02/2016");
    long difference = (date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000);
    System.out.println("Difference " + difference);

Output

Difference 366

Same for all other dates in the question.

For non-leap years it prints 365 as expected.

Saravana
  • 12,647
  • 2
  • 39
  • 57