• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

timespan false?

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two date objects - I want to get the timespan between the two dates:

return ((endTime.getTime() - startTime.getTime()) / 60*1000);


but it returns for example: 13000 (even it endures only approx 1 second). So I guess, 13000 means 1,3 sec. Is my formula false?

Should I use something like the examples described here: http://code.hammerpig.com/find-time-difference-dates-java.html:

http://code.hammerpig.com/find-time-difference-dates-java.html



Why is my formula false?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code from Hammerpig looks horrible and it does strange and unnecessary stuff with timezones.

Your formula looks correct. Are you sure your numbers are correct? What do you see if you print the content of the two Date objects:

Date.getTime() returns a number measured in milliseconds. If you divide that by 60 * 1000 you get a value in minutes. So 13000 means 13000 minutes.
 
Sheriff
Posts: 22803
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper Young wrote:If you divide that by 60 * 1000 you get a value in minutes.


Ah, but Nimo doesn't divide by 60 * 1000.
This will first calculate endTime.getTime() - startTime.getTime(); the difference in milliseconds. This is then divided by 60; the difference in milliminutes. That is then multiplied by 1000. Remember, / and * have the same operator precedence, and are therefore evaluated from left to right.

To calculate the difference in minutes the following are two options:
(first divide by 60, then divide by 1000)
(really divide by 60 * 1000 by ensuring it is evaluated before the division)

These days you should check out TimeUnit though:
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello jesper,

thanks - yes that was my fault.


hello rob,

thank you!! I did not know the TimeUnit-Class. I will use it for my problem as it works like a charm:-)

thanks!!


 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, my mistake. Thanks Rob, I didn't know the TimeUnit class - learned something today!
 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought TimeUnit is an enum.
 
Rob Spoor
Sheriff
Posts: 22803
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nitpickers!
 
permaculture is giving a gift to your future self. After reading this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic