?
Solved

Finding day difference between two dates

Posted on 2006-11-07
12
Medium Priority
?
655 Views
Last Modified: 2008-02-01
Hello,
I've been trying to figure how to correctly find the day difference between two days.
Below you will see a code I'm using to compare days. But I believe it's not functioning correctly. I'm testing it on these two dates:
2005-08-10 and 2005-02-10

The code below says the difference is 184 days.
However when I use excel's day difference function (namely days360) excel says the difference is 180 days.

I would like to learn how to find the difference correctly.
Thanks!





xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
public static int getDaysBetween (java.util.Calendar d1, java.util.Calendar d2) {
    if (d1.after(d2)) {  // swap dates so that d1 is start and d2 is end
        java.util.Calendar swap = d1;
        d1 = d2;
        d2 = swap;
    }
    int days = d2.get(java.util.Calendar.DAY_OF_YEAR) -
               d1.get(java.util.Calendar.DAY_OF_YEAR);
    int y2 = d2.get(java.util.Calendar.YEAR);
    if (d1.get(java.util.Calendar.YEAR) != y2) {
        d1 = (java.util.Calendar) d1.clone();
        do {
            days += d1.getActualMaximum(java.util.Calendar.DAY_OF_YEAR);
            d1.add(java.util.Calendar.YEAR, 1);
        } while (d1.get(java.util.Calendar.YEAR) != y2);
    }
    return days;
} // getDaysBetween()

0
Comment
Question by:halukakin79
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 3
12 Comments
 
LVL 16

Accepted Solution

by:
suprapto45 earned 2000 total points
ID: 17895612
public static int getDaysBetween (java.util.Calendar d1, java.util.Calendar d2) {
    if (d1.after(d2)) {  // swap dates so that d1 is start and d2 is end
        java.util.Calendar swap = d1;
        d1 = d2;
        d2 = swap;
    }
   
    long diffMillis = d2.getTimeInMillis()-d1.getTimeInMillis();
    long diffDays = diffMillis/(24*60*60*1000);

    return new Long(diffDays).intValue();
} // getDaysBetween()
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 17895613
Try that,

I have not tested it :)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:halukakin79
ID: 17895718
No that doesn't do it.

I'm testing it between 2005-02-10 and 2005-03-10.
Your code says there are 30 days in between.

However when I count the days from the calendar there are 28 days in between.
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 17895849
can you post how do you construct your calendar?
0
 

Author Comment

by:halukakin79
ID: 17895868
Sure thing. Here it is:

    Calendar c1 = Calendar.getInstance();
    c1.set(2005, 3 , 10);
    Calendar c2 = Calendar.getInstance();
    c2.set(2005, 2 , 10);
    getDaysBetween(c1,c2);
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 17895870
Give me a sec
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 17895887
Do you know that the month is actually starting from zero?

So FEBRUARY is 1 instead of 2

    Calendar c1 = Calendar.getInstance();
    c1.set(2005, Calendar.MARCH , 10);
    Calendar c2 = Calendar.getInstance();
    c2.set(2005, Calendar.FEBRUARY , 10);

is equal to

    Calendar c1 = Calendar.getInstance();
    c1.set(2005, 2 , 10);
    Calendar c2 = Calendar.getInstance();
    c2.set(2005, 1 , 10);
    getDaysBetween(c1,c2);
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 17895891
Use

System.out.println(c1.getTime().toString());
System.out.println(c2.getTime().toString());

to have a look what I mean

David
0
 

Author Comment

by:halukakin79
ID: 17895936
Thanks a lot! :)
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 17895942
You are welcome :)

Glad I could help

David
0
 

Expert Comment

by:mskumar_apk
ID: 17895945
long start = cal1.getTime().getTime();
long end - cal2.getTime().getTime();

long diff = (end - start)/86400;

This should work irrespective of the timezones.

Kumar
www.fwanalyzer.com
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question