• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference Between two dates

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you getting 86400 for seconds?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another thing: 3 isn't March.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was the essence of the 2nd hint...that's clearly stated in the javadoc for set().
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look on api of date class
 
Sri Yasasvi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for your help. Could you please suggest me any work around for this.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic