aspose file tools*
The moose likes Beginning Java and the fly likes Difference Between two dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference Between two dates" Watch "Difference Between two dates" New topic
Author

Difference Between two dates

Sri Yasasvi
Greenhorn

Joined: Nov 07, 2008
Posts: 28
Hi

I am trying to calculate the difference between two dates. But i am getting wrong result.

Here is my code.

public static void main(String[] args) {
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar1.set(2010, 03, 31);
calendar2.set(2010, 04, 01);
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.");
}


I should get the result as 1. But i am getting 0 days.

Could you please correct me if i am doing any thing wrong here.


Thanks in advance.

Sri
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Write down the figures and see what happens when you do integer division. Remember 1000 / 1001 comes out as 0.
Also beware of mixing int and long arithmetic; 24 *60 * 60 * 1000 is still within the range of an int, but one month (30 * 24 * 60 * 60 * 1000) is outwith that range and will cause an overflow error. Using diff / (60 * 1000) increases the risk of overflow over diff / 60 / 1000.

The difference between 10pm tonight and 1am tomorrow is 3 hours, but if you divide the milliseconds in 3 hours by (24 * 60 * 60 * 1000) you will get 0.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Are you getting 86400 for seconds?
Bert Wilkinson
Ranch Hand

Joined: Oct 28, 2009
Posts: 33
Step 1 in almost any troubleshooting:

"when in doubt, print it out..."

try adding the following lines of code, your mistake should bubble up quickly:


2nd hint: take a look at the javadoc for the Calendar.set() method.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Another thing: 3 isn't March.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Another thing: beware of starting integer literals with a 0. 03 might be the same as 3 but just try 08 for not-quite-August .
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

And another thing: not all days have 24 hours. In places with daylight saving time, there's one day a year with 23 hours and one day a year with 25 hours.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Campbell Ritchie wrote:Another thing: 3 isn't March.

Good catch. Sri, you are actually comparing April 31st to May 1st. Since Calendars are lenient by default this April 31st gets wrapped to May 1st. So in the end, you are comparing May 1st with May 1st.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bert Wilkinson
Ranch Hand

Joined: Oct 28, 2009
Posts: 33
That was the essence of the 2nd hint...that's clearly stated in the javadoc for set().
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Bert Wilkinson wrote:That was the essence of the 2nd hint...that's clearly stated in the javadoc for set().
Yes, it is, but I hadn't realised that was the hint you were dropping. It goes to show that people who ask questions ought to find the API documentation and read it: here.
raj sawant
Greenhorn

Joined: Apr 08, 2010
Posts: 3
look on api of date class
Sri Yasasvi
Greenhorn

Joined: Nov 07, 2008
Posts: 28
Thank you so much for your help. Could you please suggest me any work around for this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
You're welcome

You don't need a workaround. The people who posted on this thread have given hints, and several times told you to look at the API documentation. If you had done that, you would have worked out how to use the Calendar class correctly.

And another thing (as everybody seems to say ( ): You ought not to put numbers into your code if you can help it. Rather than writing 3 you ought to use the static fields of the Calendar class, eg Calendar.MARCH.
 
wood burning stoves
 
subject: Difference Between two dates