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.
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.
Thank you so much for your help. Could you please suggest me any work around for this.
Joined: Oct 13, 2005
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.