Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference Between two dates

 
Sri Yasasvi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you getting 86400 for seconds?
 
Bert Wilkinson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing: 3 isn't March.
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bert Wilkinson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was the essence of the 2nd hint...that's clearly stated in the javadoc for set().
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look on api of date class
 
Sri Yasasvi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for your help. Could you please suggest me any work around for this.
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic