1

After consulting a few forums, I ended up using the code below to find the days difference. But, I see a problem with the logic (may be it's my over sight?). I see that for days difference between 11 to 14 and 11 to 15 is same. How is it possible?

Date createdDate = new Date((2013 + 1900), (1 + 1), 11);
Date expirationDate = new Date((2013 + 1900), (1 + 1), 11);
for (int i = 11; i < 20; i++) {
    expirationDate.setDate(i);

    System.out.println("11 to " + i + " = "
            + (int) (expirationDate.getTime() - createdDate.getTime())
            / (1000 * 60 * 60 * 24));
}

The output is:

11 to 11 = 0
11 to 12 = 1
11 to 13 = 2
11 to 14 = 3
11 to 15 = 3
11 to 16 = 4
11 to 17 = 5
11 to 18 = 6
11 to 19 = 7
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
Farhan Syed
  • 336
  • 3
  • 15
  • Like the answer below suggests, you should use a Calendar in stead of a Date. All the methods you're using are deprecated. – jahroy Jan 11 '13 at 00:10
  • I also have to wonder if your strange output is related to the fact that you're comparing two dates from the year 3913! To create a Calendar for Jan 11th, 2013 you would do this: `Calendar c = new GregorianCalendar(1900 + 113, 2, 11);` – jahroy Jan 11 '13 at 00:16
  • Should the year matter in case of days difference? – Farhan Syed Jan 11 '13 at 00:36
  • 2
    I have no idea... Calendars are a lot more complicated than they appear on the surface. I won't even attempt to comment on what will happen with a calendar from 2000 years in the future! **HOWEVER**, now that I look at your code again I see a possible error your code: you can't use an int to represent a timestamp (it's too small). You need to use a long. An integer is only large enough to represent 1 month in milliseconds. This could certainly be the cause of your strange output. – jahroy Jan 11 '13 at 01:04
  • You are correct. My intention was to get days as int. I should have been more careful with the brackets! – Farhan Syed Jan 11 '13 at 01:07
  • possible duplicate of [Calculating the Difference Between Two Java Date Instances](http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) – Kate Gregory Jan 11 '13 at 01:23

5 Answers5

4

Use Joda Time's Days#daysBetween(). There is no better way.

DateMidnight createdDate = new DateMidnight(2013, 2, 11);

for (int i = 11; i < 20; i++) {

    DateMidnight expirationDate = new DateMidnight(2013, 2, i);
    int dayDifference = Days.daysBetween(createdDate, expirationDate);

    System.out.println("11 to " + i + " = " + dayDifference);
}
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
1

Date(year,day, month ) constructor is deprecated. i would simply use Calendars methods to get the difference between two days:

Calendar cal1=Calendar.getInstance();
Calendar cal2=Calendar.getInstance();
cal1.setTime(createdDate);
cal2.setTime(expirationDate);
System.out.println(cal2.get(Calendar.DAY_OF_MONTH )-cal1.get(Calendar.DAY_OF_MONTH ) );

EDIT:

Calendar cal1 = Calendar.getInstance();
cal1.set(2013, 2, 11);
Calendar cal2 = Calendar.getInstance();
cal2.set(2013, 2, 11);
for (int i = 11; i < 20; i++) {
 cal2.set(Calendar.DATE, i);
    System.out.println("11 to " + i + " = " + (cal2.get(Calendar.DAY_OF_MONTH) -cal1.get(Calendar.DAY_OF_MONTH)));
}

OUTPUT:

11 to 11 = 0
11 to 12 = 1
11 to 13 = 2
11 to 14 = 3
11 to 15 = 4
11 to 16 = 5
11 to 17 = 6
11 to 18 = 7
11 to 19 = 8
PermGenError
  • 45,977
  • 8
  • 87
  • 106
1

1000 * 60 * 60 * 24 is wrong way to find day difference. You can use JodaTime but there is a pure java solution.

Let we have two inialized variables

Calendar firstDay = ...;
Calendar secondDay = ...;

and

firstDay.before(lastDay)

is true.

Run

int firstDayNo = firstDay.get(Calendar.DAY_OF_YEAR);
int secondDayNo = secondDay.get(Calendar.DAY_OF_YEAR);
int dayDifference, yearMultiplier;

dayDifference = -firstDayNo;
yearMultiplier = secondDay.get(Calendar.YEAR) - firstDay.get(Calendar.YEAR);
while (yearMultiplier > 0) {
    dayDifference += firstDay.getActualMaximum(Calendar.DAY_OF_YEAR);
    firstDay.add(Calendar.YEAR, 1);
    yearMultiplier--;
}
dayDifference += secondDayNo;

return dayDifference;
Radio Rogal
  • 462
  • 4
  • 9
0

Using float, I see the problem. Using timestamp doesn't seem like a good approach to finding the days difference between 2 dates.

11 to 11 = 0.0
11 to 12 = 1.0
11 to 13 = 2.0
11 to 14 = 3.0
11 to 15 = 3.9583333
11 to 16 = 4.9583335
11 to 17 = 5.9583335
11 to 18 = 6.9583335
11 to 19 = 7.9583335

Going forward, I find the most conclusive way to determine the date difference as

Calendar cre_calendar = new GregorianCalendar((2013), (1), 11);
        Calendar exp_calendar = new GregorianCalendar((2013), (1), 19);
        Calendar maxDays = new GregorianCalendar(((2013)), (12), 31);

        if (exp_calendar.get(Calendar.DAY_OF_YEAR) < cre_calendar
                .get(Calendar.DAY_OF_YEAR)) {
            System.out
                    .println((exp_calendar.get(Calendar.DAY_OF_YEAR) + maxDays
                            .get(Calendar.DAY_OF_YEAR))
                            - cre_calendar.get(Calendar.DAY_OF_YEAR));
        } else {
            System.out.println((exp_calendar.get(Calendar.DAY_OF_YEAR))
                    - cre_calendar.get(Calendar.DAY_OF_YEAR));
        }
Farhan Syed
  • 336
  • 3
  • 15
  • 1
    Using timestamps **IS** the correct way to do it. However, you need to use a `long` to represent a timestamp (not a float or int). An integer is only large enough to capture one month of time in milliseconds. – jahroy Jan 11 '13 at 00:56
0

At least in the current version your code prints correct results

11 to 11 = 0
11 to 12 = 1
11 to 13 = 2
11 to 14 = 3
11 to 15 = 4
11 to 16 = 5
11 to 17 = 6
11 to 18 = 7
11 to 19 = 8

Nevertheless new Date((2013 + 1900), (1 + 1), 11); is incorrect, in fact it is 5813-03-01. It should be new Date((2013 - 1900), (1 - 1), 11); see Date(int year, int month, int day) API

Parameters:
year - the year minus 1900.
month - the month between 0-11.
date - the day of the month between 1-31.
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275