tags:

views:

408

answers:

6

Hi All,

I've to find number of days between two dates: one, is from report and one, is current date. My snippet :

  int age=calculateDifference(agingDate, today);

Here, calculateDifference method is a private method, agingDate and today are Date objects, just for your clarification. I've followed two articles from Java Forum Thread1 and Thread 2. It works fine in a standalone program. When I include this into my logic to read from report, I'm getting unusual difference values.

Can anyone help me, why is it happening and how can I fix it?

EDIT :

I'm getting a lot greater than the actual difference of Days...

public static int calculateDifference(Date a, Date b)
{
    int tempDifference = 0;
    int difference = 0;
    Calendar earlier = Calendar.getInstance();
    Calendar later = Calendar.getInstance();

    if (a.compareTo(b) < 0)
    {
        earlier.setTime(a);
        later.setTime(b);
    }
    else
    {
        earlier.setTime(b);
        later.setTime(a);
    }

    while (earlier.get(Calendar.YEAR) != later.get(Calendar.YEAR))
    {
        tempDifference = 365 * (later.get(Calendar.YEAR) - earlier.get(Calendar.YEAR));
        difference += tempDifference;

        earlier.add(Calendar.DAY_OF_YEAR, tempDifference);
    }

    if (earlier.get(Calendar.DAY_OF_YEAR) != later.get(Calendar.DAY_OF_YEAR))
    {
        tempDifference = later.get(Calendar.DAY_OF_YEAR) - earlier.get(Calendar.DAY_OF_YEAR);
        difference += tempDifference;

        earlier.add(Calendar.DAY_OF_YEAR, tempDifference);
    }

    return difference;
}

Thanx in advance...

Note :

Unfortunately, I couldn't get the answer this way. I've accomplished this problem with the help of Joda-time library.

A: 

You know, it that doesn't work out for you you can always can some basic math with it. But you could also google your question to see many answers achieved in different ways.

Dan
:( who's the meany head here?
Dan
+1  A: 

You say it "works fine in a standalone program," but that you get "unusual difference values" when you "include this into my logic to read from report". That suggests that your report has some values for which it doesn't work correctly, and your standalone program doesn't have those values. Instead of a standalone program, I suggest a test case. Write a test case much as you would a standalone program, subclassing from JUnit's TestCase class. Now you can run a very specific example, knowing what value you expect (and don't give it today for the test value, because today changes over time). If you put in the values you used in the standalone program, your tests will probably pass. That's great - you want those cases to keep working. Now, add a value from your report, one that doesn't work right. Your new test will probably fail. Figure out why it's failing, fix it, and get to green (all tests passing). Run your report. See what's still broken; write a test; make it pass. Pretty soon you'll find your report is working.

Carl Manaster
+5  A: 

I would suggest you use the excellent Joda Time library instead of the flawed java.util.Date and friends. You could simply write

import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.Days;

Date past = new Date(110, 5, 20); // June 20th, 2010
Date today = new Date(110, 6, 24); // July 24th 
int days = Days.daysBetween(new DateTime(past), new DateTime(today)).getDays(); // => 34
Adam Schmideg
http://joda-time.sourceforge.net/faq.html#datediff -- I was about to suggest same thing.
ghaxx
@Adam - I've Date objects, but in Joda Time API, daysBetween method takes ReadableInstant as parameter. Can u tell me how can I do it? I've no idea on those APIs. Thanx
venJava
jodaInstance = new DateTime(jdkDate);On conversion between joda time and java.util.Date and friends, see http://joda-time.sourceforge.net/userguide.html#JDK_Interoperability
Adam Schmideg
@Adam - I've tried, but getting greater value. Say for agingDate="06/22/2010", today="07/23/2010", it gives 182 days as difference.. Can you pls tell me what's going wrong???
venJava
@ven coder - I think you can post it as a separate question about using joda time. You could also provide a code snippet that gave the seemingly wrong result.
Adam Schmideg
@Adam - Then what about this question. You're telling me to ask two questions for a single problem?? Help me resolve this issue...
venJava
@ven coder - I updated the sample code, it works for me. I see this question answered. Show the code that doesn't work for you, either here, or in a separate question.
Adam Schmideg
A: 

The simplest way is to use the JODA library: http://joda-time.sourceforge.net/faq.html#datediff

Thorbjørn Ravn Andersen
+1  A: 

I might be too late to join the game but what the heck huh? :)

Do you think this is a threading issue? How are you using the output of this method for example? OR

Can we change your code to do something as simple as:

Calendar calendar1 = Calendar.getInstance();
    Calendar calendar2 = Calendar.getInstance();
    calendar1.set(<your earlier date>);
    calendar2.set(<your current date>);
    long milliseconds1 = calendar1.getTimeInMillis();
    long milliseconds2 = calendar2.getTimeInMillis();
    long diff = milliseconds2 - milliseconds1;
    long diffSeconds = diff / 1000;
    long diffMinutes = diff / (60 * 1000);
    long diffHours = diff / (60 * 60 * 1000);
    long diffDays = diff / (24 * 60 * 60 * 1000);
    System.out.println("\nThe Date Different Example");
    System.out.println("Time in milliseconds: " + diff
 + " milliseconds.");
    System.out.println("Time in seconds: " + diffSeconds
 + " seconds.");
    System.out.println("Time in minutes: " + diffMinutes 
+ " minutes.");
    System.out.println("Time in hours: " + diffHours 
+ " hours.");
    System.out.println("Time in days: " + diffDays 
+ " days.");
  }
Suji
+1  A: 

It depends on what you define as the difference. To compare two dates at midnight you can do.

long day1 = ...; // in milliseconds.
long day2 = ...; // in milliseconds.
long days = (day2 - day1) / 86400000;
Peter Lawrey