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: 32704
4
posted
0
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: 32704
4
posted
0
Are you getting 86400 for seconds?
Bert Wilkinson
Ranch Hand
Joined: Oct 28, 2009
Posts: 33
posted
0
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: 32704
4
posted
0
Another thing: 3 isn't March.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32704
4
posted
0
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 .
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.
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.
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: 32704
4
posted
0
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
posted
0
look on api of date class
Sri Yasasvi
Greenhorn
Joined: Nov 07, 2008
Posts: 27
posted
0
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: 32704
4
posted
0
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.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.